@@ -14,6 +14,7 @@ import androidx.core.os.bundleOf
1414import androidx.core.view.isInvisible
1515import androidx.core.view.isVisible
1616import androidx.lifecycle.lifecycleScope
17+ import androidx.media3.common.MediaMetadata
1718import androidx.media3.common.Player
1819import androidx.media3.session.MediaController
1920import androidx.media3.ui.PlayerView
@@ -63,9 +64,11 @@ class OfflinePlayerActivity : BaseActivity() {
6364 override fun onEvents (player : Player , events : Player .Events ) {
6465 super .onEvents(player, events)
6566 // update the displayed duration on changes
66- playerBinding.duration.text = DateUtils .formatElapsedTime(
67- player.duration / 1000
68- )
67+ if (::playerBinding.isInitialized && player.duration >= 0 ) {
68+ playerBinding.duration.text = DateUtils .formatElapsedTime(
69+ player.duration / 1000
70+ )
71+ }
6972 }
7073
7174 override fun onIsPlayingChanged (isPlaying : Boolean ) {
@@ -97,6 +100,13 @@ class OfflinePlayerActivity : BaseActivity() {
97100 )
98101 }
99102 }
103+
104+ override fun onMediaMetadataChanged (mediaMetadata : MediaMetadata ) {
105+ super .onMediaMetadataChanged(mediaMetadata)
106+
107+ mediaMetadata.extras?.getString(IntentData .videoId)?.let { videoId = it }
108+ lifecycleScope.launch { loadPlayerData() }
109+ }
100110 }
101111
102112 private val playerActionReceiver = object : BroadcastReceiver () {
@@ -194,23 +204,24 @@ class OfflinePlayerActivity : BaseActivity() {
194204 binding.playerGestureControlsView.binding,
195205 chaptersViewModel
196206 )
207+ }
197208
198- lifecycleScope.launch {
199- val (downloadInfo, downloadItems, downloadChapters) = withContext(Dispatchers .IO ) {
200- Database .downloadDao().findById(videoId)
201- }!!
209+ private suspend fun loadPlayerData () {
210+ val (downloadInfo, downloadItems, downloadChapters) = withContext(Dispatchers .IO ) {
211+ Database .downloadDao().findById(videoId)
212+ }!!
202213
203- val chapters = downloadChapters.map(DownloadChapter ::toChapterSegment)
204- chaptersViewModel.chaptersLiveData.value = chapters
205- binding.player.setChapters(chapters)
214+ val chapters = downloadChapters.map(DownloadChapter ::toChapterSegment)
215+ chaptersViewModel.chaptersLiveData.value = chapters
216+ binding.player.setChapters(chapters)
206217
207- playerBinding.exoTitle.text = downloadInfo.title
208- playerBinding.exoTitle.isVisible = true
218+ playerBinding.exoTitle.text = downloadInfo.title
219+ playerBinding.exoTitle.isVisible = true
209220
210- timeFrameReceiver = downloadItems.firstOrNull { it.path.exists() && it.type == FileType .VIDEO }?.path?.let {
221+ timeFrameReceiver =
222+ downloadItems.firstOrNull { it.path.exists() && it.type == FileType .VIDEO }?.path?.let {
211223 OfflineTimeFrameReceiver (this @OfflinePlayerActivity, it)
212224 }
213- }
214225 }
215226
216227 override fun onResume () {
@@ -228,7 +239,10 @@ class OfflinePlayerActivity : BaseActivity() {
228239 }
229240
230241 override fun onDestroy () {
231- playerController.sendCustomCommand(AbstractPlayerService .stopServiceCommand, Bundle .EMPTY )
242+ playerController.sendCustomCommand(
243+ AbstractPlayerService .stopServiceCommand,
244+ Bundle .EMPTY
245+ )
232246
233247 runCatching {
234248 unregisterReceiver(playerActionReceiver)
0 commit comments