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: Null intent when reopened from recents #1192

Merged
merged 2 commits into from
Dec 28, 2024
Merged
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 @@ -130,6 +130,7 @@ class PlayerActivity : AppCompatActivity() {
private var hideInfoLayoutJob: Job? = null

private var playInBackground: Boolean = false
private var isIntentNew: Boolean = true

private val shouldFastSeek: Boolean
get() = playerPreferences.shouldFastSeek(mediaController?.duration ?: C.TIME_UNSET)
Expand Down Expand Up @@ -343,10 +344,7 @@ class PlayerActivity : AppCompatActivity() {
addListener(playbackStateListener)
volumeManager.loudnessEnhancer = loudnessEnhancer

if (intent.data != null && intent.data.toString() != currentMediaItem?.mediaId) {
playVideo(uri = viewModel.currentMediaItemUri ?: intent.data!!)
intent.data = null
}
startPlayback()
}
subtitleFileLauncherLaunchedForMediaItem = null
}
Expand Down Expand Up @@ -650,7 +648,21 @@ class PlayerActivity : AppCompatActivity() {
}
}

private fun playVideo(uri: Uri) = lifecycleScope.launch(Dispatchers.IO) {
private fun startPlayback(uri: Uri = intent.data!!) {
// If the intent is not new and the current media item is not null, return
if (!isIntentNew && mediaController?.currentMediaItem != null) return

// If the current media item is not null and the current media item's uri is the same as the intent's data, return
if (mediaController?.currentMediaItem?.localConfiguration?.uri.toString() == intent.data.toString()) return

isIntentNew = false

lifecycleScope.launch {
playVideo(uri)
}
}

private suspend fun playVideo(uri: Uri) = withContext(Dispatchers.Default) {
val mediaContentUri = getMediaContentUri(uri)
val playlist = mediaContentUri?.let { mediaUri ->
viewModel.getPlaylistFromUri(mediaUri)
Expand Down Expand Up @@ -697,7 +709,7 @@ class PlayerActivity : AppCompatActivity() {
private fun playbackStateListener() = object : Player.Listener {
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
super.onMediaItemTransition(mediaItem, reason)
viewModel.currentMediaItemUri = mediaItem?.localConfiguration?.uri
intent.data = mediaItem?.localConfiguration?.uri
isMediaItemReady = false
}

Expand Down Expand Up @@ -795,10 +807,10 @@ class PlayerActivity : AppCompatActivity() {
super.onNewIntent(intent)
if (intent.data != null) {
currentOrientation = null
viewModel.currentMediaItemUri = intent.data
setIntent(intent)
isIntentNew = true
if (mediaController != null) {
playVideo(intent.data!!)
intent.data = null
startPlayback()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class PlayerViewModel @Inject constructor(
private val getSortedPlaylistUseCase: GetSortedPlaylistUseCase,
) : ViewModel() {

var currentMediaItemUri: Uri? = null
var playWhenReady: Boolean = true
var skipSilenceEnabled: Boolean = false

Expand Down
Loading