mirror of
https://github.com/JustinZeus/Wholphin.git
synced 2026-07-09 16:11:20 +02:00
Fix issue where theme songs played when the app is backgrounded (#34)
Previously if the app is playing a show's theme song and the app is backgrounded, such as by pressing Home, the music would continue. This PR fixes that.
This commit is contained in:
parent
5bb46bf34b
commit
93f7abfa3b
3 changed files with 82 additions and 39 deletions
|
|
@ -0,0 +1,68 @@
|
|||
package com.github.damontecres.wholphin.util
|
||||
|
||||
import android.content.Context
|
||||
import androidx.annotation.OptIn
|
||||
import androidx.media3.common.MediaItem
|
||||
import androidx.media3.common.Player
|
||||
import androidx.media3.common.util.UnstableApi
|
||||
import androidx.media3.datasource.okhttp.OkHttpDataSource
|
||||
import androidx.media3.exoplayer.ExoPlayer
|
||||
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
|
||||
import com.github.damontecres.wholphin.hilt.AuthOkHttpClient
|
||||
import com.github.damontecres.wholphin.preferences.ThemeSongVolume
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import okhttp3.OkHttpClient
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* Simple service to play theme song music
|
||||
*/
|
||||
@Singleton
|
||||
class ThemeSongPlayer
|
||||
@Inject
|
||||
constructor(
|
||||
@param:ApplicationContext val context: Context,
|
||||
@param:AuthOkHttpClient val okHttpClient: OkHttpClient,
|
||||
) {
|
||||
private var player: Player? = null
|
||||
|
||||
@OptIn(UnstableApi::class)
|
||||
fun play(
|
||||
volume: ThemeSongVolume,
|
||||
url: String,
|
||||
) {
|
||||
val volumeLevel =
|
||||
when (volume) {
|
||||
ThemeSongVolume.UNRECOGNIZED,
|
||||
ThemeSongVolume.DISABLED,
|
||||
-> return
|
||||
|
||||
ThemeSongVolume.LOWEST -> .05f
|
||||
ThemeSongVolume.LOW -> .1f
|
||||
ThemeSongVolume.MEDIUM -> .25f
|
||||
ThemeSongVolume.HIGH -> .5f
|
||||
ThemeSongVolume.HIGHEST -> 75f
|
||||
}
|
||||
val player =
|
||||
ExoPlayer
|
||||
.Builder(context)
|
||||
.setMediaSourceFactory(
|
||||
DefaultMediaSourceFactory(
|
||||
OkHttpDataSource.Factory(okHttpClient),
|
||||
),
|
||||
).build()
|
||||
.apply {
|
||||
this.volume = volumeLevel
|
||||
playWhenReady = true
|
||||
}
|
||||
player.setMediaItem(MediaItem.fromUri(url))
|
||||
player.prepare()
|
||||
this.player = player
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
player?.stop()
|
||||
player?.release()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue