Merge pull request #3877 from Bnyro/master

Fix crashes when audio and video mode running simultaneously
This commit is contained in:
Bnyro 2023-06-01 18:16:09 +02:00 committed by GitHub
commit d0c86fcc3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 22 deletions

View File

@ -31,20 +31,19 @@ object BackgroundHelper {
) { ) {
// close the previous video player if open // close the previous video player if open
if (!keepVideoPlayerAlive) { if (!keepVideoPlayerAlive) {
(context as? MainActivity)?.supportFragmentManager?.let { fragmentManager -> val fragmentManager = ContextHelper.unwrapActivity(context).supportFragmentManager
fragmentManager.fragments.firstOrNull { it is PlayerFragment }?.let { fragmentManager.fragments.firstOrNull { it is PlayerFragment }?.let {
fragmentManager.commit { remove(it) } fragmentManager.commit { remove(it) }
} }
} }
}
// create an intent for the background mode service // create an intent for the background mode service
val intent = Intent(context, OnlinePlayerService::class.java) val intent = Intent(context, OnlinePlayerService::class.java)
intent.putExtra(IntentData.videoId, videoId) .putExtra(IntentData.videoId, videoId)
intent.putExtra(IntentData.playlistId, playlistId) .putExtra(IntentData.playlistId, playlistId)
intent.putExtra(IntentData.channelId, channelId) .putExtra(IntentData.channelId, channelId)
intent.putExtra(IntentData.position, position) .putExtra(IntentData.position, position)
intent.putExtra(IntentData.keepQueue, keepQueue) .putExtra(IntentData.keepQueue, keepQueue)
// start the background mode as foreground service // start the background mode as foreground service
ContextCompat.startForegroundService(context, intent) ContextCompat.startForegroundService(context, intent)

View File

@ -0,0 +1,15 @@
package com.github.libretube.helpers
import android.content.Context
import android.content.ContextWrapper
import com.github.libretube.ui.activities.MainActivity
object ContextHelper {
fun unwrapActivity(context: Context): MainActivity {
var correctContext: Context? = context
while (correctContext !is MainActivity && correctContext is ContextWrapper) {
correctContext = correctContext.baseContext
}
return correctContext as MainActivity
}
}

View File

@ -31,7 +31,7 @@ object NavigationHelper {
) { ) {
if (channelId == null) return if (channelId == null) return
val activity = unwrapActivity(context) val activity = ContextHelper.unwrapActivity(context)
val bundle = bundleOf(IntentData.channelId to channelId) val bundle = bundleOf(IntentData.channelId to channelId)
activity.navController.navigate(R.id.channelFragment, bundle) activity.navController.navigate(R.id.channelFragment, bundle)
try { try {
@ -45,14 +45,6 @@ object NavigationHelper {
} }
} }
private fun unwrapActivity(context: Context): MainActivity {
var correctContext: Context? = context
while (correctContext !is MainActivity && correctContext is ContextWrapper) {
correctContext = correctContext.baseContext
}
return correctContext as MainActivity
}
/** /**
* Navigate to the given video using the other provided parameters as well * Navigate to the given video using the other provided parameters as well
* If the audio only mode is enabled, play it in the background, else as a normal video * If the audio only mode is enabled, play it in the background, else as a normal video
@ -67,9 +59,9 @@ object NavigationHelper {
forceVideo: Boolean = false, forceVideo: Boolean = false,
) { ) {
if (videoId == null) return if (videoId == null) return
BackgroundHelper.stopBackgroundPlay(context)
if (PreferenceHelper.getBoolean(PreferenceKeys.AUDIO_ONLY_MODE, false) && !forceVideo) { if (PreferenceHelper.getBoolean(PreferenceKeys.AUDIO_ONLY_MODE, false) && !forceVideo) {
BackgroundHelper.stopBackgroundPlay(context)
BackgroundHelper.playOnBackground( BackgroundHelper.playOnBackground(
context, context,
videoId.toID(), videoId.toID(),
@ -92,7 +84,7 @@ object NavigationHelper {
IntentData.timeStamp to timeStamp, IntentData.timeStamp to timeStamp,
) )
val activity = unwrapActivity(context) val activity = ContextHelper.unwrapActivity(context)
activity.supportFragmentManager.commitNow { activity.supportFragmentManager.commitNow {
replace<PlayerFragment>(R.id.container, args = bundle) replace<PlayerFragment>(R.id.container, args = bundle)
} }
@ -105,7 +97,7 @@ object NavigationHelper {
) { ) {
if (playlistId == null) return if (playlistId == null) return
val activity = unwrapActivity(context) val activity = ContextHelper.unwrapActivity(context)
val bundle = bundleOf( val bundle = bundleOf(
IntentData.playlistId to playlistId, IntentData.playlistId to playlistId,
IntentData.playlistType to playlistType, IntentData.playlistType to playlistType,
@ -117,7 +109,7 @@ object NavigationHelper {
* Start the audio player fragment * Start the audio player fragment
*/ */
fun startAudioPlayer(context: Context) { fun startAudioPlayer(context: Context) {
val activity = unwrapActivity(context) val activity = ContextHelper.unwrapActivity(context)
activity.supportFragmentManager.commitNow { activity.supportFragmentManager.commitNow {
replace<AudioPlayerFragment>(R.id.container) replace<AudioPlayerFragment>(R.id.container)
} }