我目前正在将Chromcast集成到我们的应用程序中。我们使用Jetpack Compose来处理所有的UI元素。我们正在使用TopAppBar可组合,我正试图使用MediaRouteActionProvider将Chromecast按钮添加到其中。我能找到的使用MediaRouteActionProvider的唯一方法是使用menu.xml并在onCreateOptionsMenu中膨胀菜单。
有人知道在Jetpack Compose的菜单上下文之外使用ActionProvider的方法吗?或者我现在还在使用菜单吗?
更新:决定对此进行另一次破解。
import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.mediarouter.app.MediaRouteDialogFactory
import androidx.mediarouter.media.MediaControlIntent
import androidx.mediarouter.media.MediaRouteSelector
import com.google.android.gms.cast.framework.CastContext
import com.google.android.gms.cast.framework.CastState
import com.google.android.gms.cast.framework.CastStateListener
class MediaRouteViewModel(context: Context) : ViewModel(), CastStateListener {
var isCastingAvailable = false
private set
val castingStateLiveData = MutableLiveData(CastingState.UNKNOWN)
val mDialogFactory = MediaRouteDialogFactory.getDefault()
val mSelector: MediaRouteSelector = MediaRouteSelector.Builder()
.addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO)
.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
.build()
init {
try {
isCastingAvailable = true
val castingContext = CastContext.getSharedInstance(context)
castingContext.addCastStateListener(this)
} catch (e: Exception) {
// handle me please
}
}
fun shouldShowChooserFragment(): Boolean {
return when (castingStateLiveData.value) {
CastingState.NOT_CONNECTED -> true
CastingState.UNKNOWN -> true
CastingState.NO_DEVICES_AVAILABLE -> true
CastingState.CONNECTING -> false
CastingState.CONNECTED -> false
else -> false
}
}
enum class CastingState {
CONNECTED,
CONNECTING,
NOT_CONNECTED,
NO_DEVICES_AVAILABLE,
UNKNOWN
}
class Factory(val context: Context) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return MediaRouteViewModel(context = context) as T
}
}
override fun onCastStateChanged(p0: Int) {
val castingState = when (p0) {
CastState.CONNECTED -> CastingState.CONNECTED
CastState.CONNECTING -> CastingState.CONNECTING
CastState.NOT_CONNECTED -> CastingState.NOT_CONNECTED
CastState.NO_DEVICES_AVAILABLE -> CastingState.NO_DEVICES_AVAILABLE
else -> CastingState.UNKNOWN
}
castingStateLiveData.postValue(castingState)
}
}
@Composable
fun MediaRouter(
modifier: Modifier,
iconWidth: Dp,
fragmentManager: FragmentManager
) {
val context = LocalContext.current
val mediaRouteProviderViewModel: MediaRouteViewModel =
viewModel(factory = MediaRouteViewModel.Factory(context))
// Can use the casting state to change the painter accordingly
val castingState = mediaRouteProviderViewModel.castingStateLiveData.observeAsState()
if (mediaRouteProviderViewModel.isCastingAvailable) {
Box(modifier = modifier.size(iconWidth)) {
Image(
painter = painterResource(id = R.drawable.ic_baseline_cast_connected_24),
contentDescription = null,
modifier = Modifier
.clickable {
val shouldShowChooserFragment = mediaRouteProviderViewModel.shouldShowChooserFragment()
val fragmentTag = if (shouldShowChooserFragment) "MediaRouteChooserDialogFragment" else "MediaRouteControllerDialogFragment"
val fragment = if (shouldShowChooserFragment) {
mediaRouteProviderViewModel.mDialogFactory
.onCreateChooserDialogFragment()
.apply {
routeSelector = mediaRouteProviderViewModel.mSelector
}
} else {
mediaRouteProviderViewModel.mDialogFactory.onCreateControllerDialogFragment()
}
val transaction = fragmentManager.beginTransaction()
transaction.add(fragment, fragmentTag)
transaction.commitAllowingStateLoss()
},
contentScale = ContentScale.FillBounds
)
}
}
}
老帖子:
我相信在写作中有更好的方法,但这至少能完成任务。
class MediaRouteViewModel(context: Context) : ViewModel() {
private val mediaRouteActionProvider = MediaRouteActionProvider(context)
val buttonView: View
init {
buttonView = mediaRouteActionProvider.onCreateActionView()
mediaRouteActionProvider.routeSelector = MediaRouteSelector.Builder()
.addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO)
.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
.build()
}
fun onClick() {
mediaRouteActionProvider.onPerformDefaultAction()
}
}
@Composable
fun MediaRouter(modifier: Modifier, iconWidth: Dp) {
val context = LocalContext.current
val mediaRouteProviderViewModel = MediaRouteViewModel(context)
Box(modifier = modifier.size(iconWidth)) {
AndroidView(factory = {
mediaRouteProviderViewModel.buttonView
}, modifier = Modifier.fillMaxSize())
Box(modifier = Modifier
.fillMaxSize()
.clickable { mediaRouteProviderViewModel.onClick() })
}
}