当我尝试将 HLS 视频从我的应用程序投射到 chromecast 时发生崩溃。我使用 ExoPlayer cast 扩展。
我正在使用 exoplayer 版本2.11.1
和播放服务演员框架版本18.0.0
。该代码是从动态模块中调用的(apply plugin: 'com.android.dynamic-feature'
在模块中build.gradle
)。这是我的实现:
当我们创建 Fragment 时:
private fun initCast() {
mediaRouteButton = view?.findViewById(R.id.media_route_button)
CastButtonFactory.setUpMediaRouteButton(activity?.applicationContext, mediaRouteButton)
val castContext = CastContext.getSharedInstance()
castContext?.addCastStateListener { state ->
if (isAdded) {
if (state == CastState.NO_DEVICES_AVAILABLE) {
isCastAvailable = false
} else {
isCastAvailable = true
mediaRouteButton?.visibility = videoTitle.visibility
}
}
}
}
当用户单击 Cast 按钮时:
private fun prepareCast(title: String) {
CastContext.getSharedInstance()?.let {
castPlayer = CastPlayer(it)
}
val movieMetadata = MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE)
movieMetadata.putString(MediaMetadata.KEY_TITLE, title)
videoThumbnail?.let {
movieMetadata.addImage(WebImage(Uri.parse(it)))
}
val mediaInfo = MediaInfo.Builder(videoUrl)
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
.setContentType(MimeTypes.VIDEO_UNKNOWN)
.setMetadata(movieMetadata).build()
val mediaItems = arrayOf(MediaQueueItem.Builder(mediaInfo).build())
castPlayer?.setSessionAvailabilityListener(object : SessionAvailabilityListener {
override fun onCastSessionAvailable() {
castPlayer?.loadItems(mediaItems, 0, playerViewModel.getVideoPosition(), Player.REPEAT_MODE_OFF)
playerViewModel.pauseVideo()
}
override fun onCastSessionUnavailable() {
castPlayer?.currentPosition?.let {
playerViewModel.setVideoPosition(it)
playerViewModel.playVideo()
}
}
})
}
当用户单击我的应用程序中的投射按钮,然后在对话框中他们想要投射的设备上单击时,应用程序会系统地崩溃。这是堆栈跟踪:
FATAL EXCEPTION: main
Process: media.brut.brut.debug, PID: 28644
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/api/HasApiKey;
at com.google.android.gms.internal.cast.zzr.zza(com.google.android.gms:play-services-cast-framework@@18.0.0:8)
at com.google.android.gms.cast.framework.CastSession.zzc(com.google.android.gms:play-services-cast-framework@@18.0.0:90)
at com.google.android.gms.cast.framework.CastSession.start(com.google.android.gms:play-services-cast-framework@@18.0.0:15)
at com.google.android.gms.cast.framework.Session$zza.start(com.google.android.gms:play-services-cast-framework@@18.0.0:6)
at com.google.android.gms.cast.framework.zzad.dispatchTransaction(com.google.android.gms:play-services-cast-framework@@18.0.0:17)
at com.google.android.gms.internal.cast.zza.onTransact(com.google.android.gms:play-services-cast@@18.0.0:13)
at android.os.Binder.transact(Binder.java:914)
at jx.b(:com.google.android.gms.dynamite_dynamitemodulesc@19831081@19.8.31 (120400-0):14)
at wb.b(:com.google.android.gms.dynamite_dynamitemodulesc@19831081@19.8.31 (120400-0):32)
at wk.a(:com.google.android.gms.dynamite_dynamitemodulesc@19831081@19.8.31 (120400-0):46)
at wv.c(:com.google.android.gms.dynamite_dynamitemodulesc@19831081@19.8.31 (120400-0):21)
at wt.a(:com.google.android.gms.dynamite_dynamitemodulesc@19831081@19.8.31 (120400-0):11)
at jy.onTransact(:com.google.android.gms.dynamite_dynamitemodulesc@19831081@19.8.31 (120400-0):5)
at android.os.Binder.transact(Binder.java:914)
at com.google.android.gms.internal.cast.zzb.zzb(com.google.android.gms:play-services-cast@@18.0.0:21)
at com.google.android.gms.internal.cast.zzaf.zzd(com.google.android.gms:play-services-cast-framework@@18.0.0:21)
at com.google.android.gms.internal.cast.zzaq.onRouteSelected(com.google.android.gms:play-services-cast-framework@@18.0.0:4)
at androidx.mediarouter.media.MediaRouter$GlobalMediaRouter$CallbackHandler.invokeCallback(MediaRouter.java:3218)
at androidx.mediarouter.media.MediaRouter$GlobalMediaRouter$CallbackHandler.handleMessage(MediaRouter.java:3168)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
该应用程序仅在 Pixel 3 和 Pixel 3a 上崩溃,但在三星 S9 上不会崩溃。
有人可以告诉我我错过了什么吗?