diff --git a/course/src/main/java/org/openedx/course/presentation/unit/video/EncodedVideoUnitViewModel.kt b/course/src/main/java/org/openedx/course/presentation/unit/video/EncodedVideoUnitViewModel.kt index dec6f70e9..6e2d8d636 100644 --- a/course/src/main/java/org/openedx/course/presentation/unit/video/EncodedVideoUnitViewModel.kt +++ b/course/src/main/java/org/openedx/course/presentation/unit/video/EncodedVideoUnitViewModel.kt @@ -53,6 +53,10 @@ class EncodedVideoUnitViewModel( val isVideoEnded: LiveData get() = _isVideoEnded + private val _isCastReady = MutableLiveData(false) + val isCastReady: LiveData + get() = _isCastReady + var exoPlayer: ExoPlayer? = null private set @@ -107,13 +111,16 @@ class EncodedVideoUnitViewModel( CastContext.getSharedInstance(context, executor).addOnCompleteListener { it.result?.let { castContext -> castPlayer = CastPlayer(castContext) + _isCastReady.value = true } } } + @androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class) override fun onResume(owner: LifecycleOwner) { super.onResume(owner) exoPlayer?.addListener(exoPlayerListener) + castPlayer?.addListener(exoPlayerListener) getActivePlayer()?.playWhenReady = isPlaying } diff --git a/course/src/main/java/org/openedx/course/presentation/unit/video/VideoUnitFragment.kt b/course/src/main/java/org/openedx/course/presentation/unit/video/VideoUnitFragment.kt index d92b3b067..8c543be27 100644 --- a/course/src/main/java/org/openedx/course/presentation/unit/video/VideoUnitFragment.kt +++ b/course/src/main/java/org/openedx/course/presentation/unit/video/VideoUnitFragment.kt @@ -180,6 +180,11 @@ class VideoUnitFragment : Fragment(R.layout.fragment_video_unit) { appReviewManager.tryToOpenRateDialog() } } + viewModel.isCastReady.observe(viewLifecycleOwner) { isReady -> + if (isReady && !viewModel.isCastActive) { + setCastSessionListener() + } + } } @androidx.annotation.OptIn(UnstableApi::class) @@ -205,33 +210,6 @@ class VideoUnitFragment : Fragment(R.layout.fragment_video_unit) { viewModel.isPlayerSetUp = true } - viewModel.castPlayer?.setSessionAvailabilityListener( - object : SessionAvailabilityListener { - override fun onCastSessionAvailable() { - viewModel.logCastConnection(CourseAnalyticsEvent.CAST_CONNECTED) - viewModel.isCastActive = true - viewModel.exoPlayer?.pause() - playerView.player = viewModel.castPlayer - viewModel.castPlayer?.setMediaItem( - mediaItem, - viewModel.exoPlayer?.currentPosition ?: 0L - ) - viewModel.castPlayer?.playWhenReady = false - showVideoControllerIndefinitely(true) - } - - override fun onCastSessionUnavailable() { - viewModel.logCastConnection(CourseAnalyticsEvent.CAST_DISCONNECTED) - viewModel.isCastActive = false - playerView.player = viewModel.exoPlayer - viewModel.exoPlayer?.seekTo(viewModel.castPlayer?.currentPosition ?: 0L) - viewModel.castPlayer?.stop() - viewModel.exoPlayer?.play() - showVideoControllerIndefinitely(false) - } - } - ) - playerView.setFullscreenButtonClickListener { if (viewModel.isCastActive) return@setFullscreenButtonClickListener @@ -285,6 +263,44 @@ class VideoUnitFragment : Fragment(R.layout.fragment_video_unit) { } } + @androidx.annotation.OptIn(UnstableApi::class) + private fun setCastSessionListener() { + val movieMetadata = MediaMetadata.Builder() + .setMediaType(MediaMetadata.MEDIA_TYPE_MOVIE) + .build() + val mediaItem = MediaItem.Builder().setMediaMetadata(movieMetadata) + .setUri(viewModel.videoUrl) + .setMimeType("video/*") + .build() + + viewModel.castPlayer?.setSessionAvailabilityListener( + object : SessionAvailabilityListener { + override fun onCastSessionAvailable() { + viewModel.logCastConnection(CourseAnalyticsEvent.CAST_CONNECTED) + viewModel.isCastActive = true + viewModel.exoPlayer?.pause() + binding.playerView.player = viewModel.castPlayer + viewModel.castPlayer?.setMediaItem( + mediaItem, + viewModel.exoPlayer?.currentPosition ?: 0L + ) + viewModel.castPlayer?.playWhenReady = false + showVideoControllerIndefinitely(true) + } + + override fun onCastSessionUnavailable() { + viewModel.logCastConnection(CourseAnalyticsEvent.CAST_DISCONNECTED) + viewModel.isCastActive = false + binding.playerView.player = viewModel.exoPlayer + viewModel.exoPlayer?.seekTo(viewModel.castPlayer?.currentPosition ?: 0L) + viewModel.castPlayer?.stop() + viewModel.exoPlayer?.play() + showVideoControllerIndefinitely(false) + } + } + ) + } + companion object { private const val ARG_BLOCK_ID = "blockId" private const val ARG_VIDEO_URL = "videoUrl"