mirror of
https://github.com/JustinZeus/Wholphin.git
synced 2026-07-09 16:11:20 +02:00
Few UI fixes especially backgrounding the app (#39)
Ensures playback (both theme song & video) is stopped when the app is backgrounded, like hitting the home button. Fixes a UI glitch when marking a series as played.
This commit is contained in:
parent
63baf7f904
commit
c3cc284238
13 changed files with 143 additions and 52 deletions
|
|
@ -0,0 +1,49 @@
|
|||
package com.github.damontecres.wholphin.util
|
||||
|
||||
import android.content.Context
|
||||
import androidx.annotation.OptIn
|
||||
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.hilt.StandardOkHttpClient
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import okhttp3.OkHttpClient
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class PlayerFactory
|
||||
@Inject
|
||||
constructor(
|
||||
@param:ApplicationContext private val context: Context,
|
||||
@param:StandardOkHttpClient private val standardOkHttpClient: OkHttpClient,
|
||||
@param:AuthOkHttpClient private val authOkHttpClient: OkHttpClient,
|
||||
) {
|
||||
private var player: Player? = null
|
||||
|
||||
@OptIn(UnstableApi::class)
|
||||
fun create(useAuth: Boolean): Player {
|
||||
release()
|
||||
val player =
|
||||
ExoPlayer
|
||||
.Builder(context)
|
||||
.setMediaSourceFactory(
|
||||
DefaultMediaSourceFactory(
|
||||
OkHttpDataSource.Factory(if (useAuth) authOkHttpClient else standardOkHttpClient),
|
||||
),
|
||||
).build()
|
||||
this.player = player
|
||||
return player
|
||||
}
|
||||
|
||||
fun release() {
|
||||
player?.let {
|
||||
it.stop()
|
||||
it.release()
|
||||
}
|
||||
player = null
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +1,9 @@
|
|||
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
|
||||
|
||||
|
|
@ -22,11 +14,8 @@ import javax.inject.Singleton
|
|||
class ThemeSongPlayer
|
||||
@Inject
|
||||
constructor(
|
||||
@param:ApplicationContext val context: Context,
|
||||
@param:AuthOkHttpClient val okHttpClient: OkHttpClient,
|
||||
val playerFactory: PlayerFactory,
|
||||
) {
|
||||
private var player: Player? = null
|
||||
|
||||
@OptIn(UnstableApi::class)
|
||||
fun play(
|
||||
volume: ThemeSongVolume,
|
||||
|
|
@ -46,25 +35,17 @@ class ThemeSongPlayer
|
|||
ThemeSongVolume.HIGHEST -> 75f
|
||||
}
|
||||
val player =
|
||||
ExoPlayer
|
||||
.Builder(context)
|
||||
.setMediaSourceFactory(
|
||||
DefaultMediaSourceFactory(
|
||||
OkHttpDataSource.Factory(okHttpClient),
|
||||
),
|
||||
).build()
|
||||
playerFactory
|
||||
.create(true)
|
||||
.apply {
|
||||
this.volume = volumeLevel
|
||||
playWhenReady = true
|
||||
}
|
||||
player.setMediaItem(MediaItem.fromUri(url))
|
||||
player.prepare()
|
||||
this.player = player
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
player?.stop()
|
||||
player?.release()
|
||||
player = null
|
||||
playerFactory.release()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -129,7 +129,7 @@ class TrackActivityPlaybackListener(
|
|||
withContext(Dispatchers.Main) {
|
||||
(if (position >= 0) position else player.currentPosition).milliseconds
|
||||
}
|
||||
// Timber.v("saveActivity: itemId=$itemId, pos=$calcPosition")
|
||||
Timber.v("saveActivity: itemId=${itemPlayback.itemId}, pos=$calcPosition")
|
||||
api.playStateApi.reportPlaybackProgress(
|
||||
PlaybackProgressInfo(
|
||||
itemId = itemPlayback.itemId,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue