Merge pull request #1299 from Bnyro/master

don't block UI thread & fix crash when hls eq null
This commit is contained in:
Bnyro 2022-09-15 15:54:32 +02:00 committed by GitHub
commit 5c60bec2dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -34,7 +34,6 @@ import com.google.android.exoplayer2.audio.AudioAttributes
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
/** /**
* Loads the selected videos audio in background mode with a notification area. * Loads the selected videos audio in background mode with a notification area.
@ -91,6 +90,8 @@ class BackgroundMode : Service() {
*/ */
private val autoplay = PreferenceHelper.getBoolean(PreferenceKeys.AUTO_PLAY, true) private val autoplay = PreferenceHelper.getBoolean(PreferenceKeys.AUTO_PLAY, true)
private val handler = Handler(Looper.getMainLooper())
/** /**
* Setting the required [Notification] for running as a foreground service * Setting the required [Notification] for running as a foreground service
*/ */
@ -129,7 +130,7 @@ class BackgroundMode : Service() {
autoPlayHelper = AutoPlayHelper(playlistId) autoPlayHelper = AutoPlayHelper(playlistId)
// play the audio in the background // play the audio in the background
playAudio(videoId, position) loadAudio(videoId, position)
} catch (e: Exception) { } catch (e: Exception) {
onDestroy() onDestroy()
} }
@ -139,49 +140,57 @@ class BackgroundMode : Service() {
/** /**
* Gets the video data and prepares the [player]. * Gets the video data and prepares the [player].
*/ */
private fun playAudio( private fun loadAudio(
videoId: String, videoId: String,
seekToPosition: Long = 0 seekToPosition: Long = 0
) { ) {
// append the video to the playing queue // append the video to the playing queue
Globals.playingQueue += videoId Globals.playingQueue += videoId
runBlocking { CoroutineScope(Dispatchers.IO).launch {
val job = launch { try {
streams = RetrofitInstance.api.getStreams(videoId) streams = RetrofitInstance.api.getStreams(videoId)
} } catch (e: Exception) {
// Wait until the job is done, to load correctly later in the player return@launch
job.join()
initializePlayer()
setMediaItem()
// create the notification
if (!this@BackgroundMode::nowPlayingNotification.isInitialized) {
nowPlayingNotification = NowPlayingNotification(this@BackgroundMode, player!!)
}
nowPlayingNotification.updatePlayerNotification(streams!!)
player?.apply {
playWhenReady = playWhenReadyPlayer
prepare()
} }
// seek to the previous position if available handler.post {
if (seekToPosition != 0L) player?.seekTo(seekToPosition) playAudio(seekToPosition)
}
// set the playback speed
val playbackSpeed = PreferenceHelper.getString(
PreferenceKeys.BACKGROUND_PLAYBACK_SPEED,
"1"
).toFloat()
player?.setPlaybackSpeed(playbackSpeed)
fetchSponsorBlockSegments()
if (autoplay) setNextStream()
} }
} }
private fun playAudio(
seekToPosition: Long
) {
initializePlayer()
setMediaItem()
// create the notification
if (!this@BackgroundMode::nowPlayingNotification.isInitialized) {
nowPlayingNotification = NowPlayingNotification(this@BackgroundMode, player!!)
}
nowPlayingNotification.updatePlayerNotification(streams!!)
player?.apply {
playWhenReady = playWhenReadyPlayer
prepare()
}
// seek to the previous position if available
if (seekToPosition != 0L) player?.seekTo(seekToPosition)
// set the playback speed
val playbackSpeed = PreferenceHelper.getString(
PreferenceKeys.BACKGROUND_PLAYBACK_SPEED,
"1"
).toFloat()
player?.setPlaybackSpeed(playbackSpeed)
fetchSponsorBlockSegments()
if (autoplay) setNextStream()
}
/** /**
* create the player * create the player
*/ */
@ -244,7 +253,7 @@ class BackgroundMode : Service() {
// play new video on background // play new video on background
this.videoId = nextStreamId!! this.videoId = nextStreamId!!
this.segmentData = null this.segmentData = null
playAudio(videoId) loadAudio(videoId)
} }
/** /**
@ -252,7 +261,19 @@ class BackgroundMode : Service() {
*/ */
private fun setMediaItem() { private fun setMediaItem() {
streams?.let { streams?.let {
val mediaItem = MediaItem.Builder().setUri(it.hls!!).build() val uri = if (streams!!.hls != null) {
streams!!.hls
} else if (streams!!.audioStreams!!.isNotEmpty()) {
PlayerHelper.getAudioSource(
this,
streams!!.audioStreams!!
)
} else {
return
}
val mediaItem = MediaItem.Builder()
.setUri(uri)
.build()
player?.setMediaItem(mediaItem) player?.setMediaItem(mediaItem)
} }
} }