Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: chromecast #368

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class EncodedVideoUnitViewModel(
val isVideoEnded: LiveData<Boolean>
get() = _isVideoEnded

private val _isCastReady = MutableLiveData(false)
val isCastReady: LiveData<Boolean>
get() = _isCastReady

var exoPlayer: ExoPlayer? = null
private set

Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down
Loading