Fix playback issues from c3cc284

This commit is contained in:
Damontecres 2025-10-19 19:37:29 -04:00
parent a31cdc79e1
commit 95a74b73ff
No known key found for this signature in database
3 changed files with 27 additions and 60 deletions

View file

@ -15,6 +15,7 @@ import androidx.media3.common.Player
import androidx.media3.common.TrackSelectionOverride import androidx.media3.common.TrackSelectionOverride
import androidx.media3.common.Tracks import androidx.media3.common.Tracks
import androidx.media3.common.util.UnstableApi import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import com.github.damontecres.wholphin.data.ItemPlaybackDao import com.github.damontecres.wholphin.data.ItemPlaybackDao
import com.github.damontecres.wholphin.data.ItemPlaybackRepository import com.github.damontecres.wholphin.data.ItemPlaybackRepository
import com.github.damontecres.wholphin.data.ServerRepository import com.github.damontecres.wholphin.data.ServerRepository
@ -37,7 +38,6 @@ import com.github.damontecres.wholphin.util.EqualityMutableLiveData
import com.github.damontecres.wholphin.util.ExceptionHandler import com.github.damontecres.wholphin.util.ExceptionHandler
import com.github.damontecres.wholphin.util.LoadingExceptionHandler import com.github.damontecres.wholphin.util.LoadingExceptionHandler
import com.github.damontecres.wholphin.util.LoadingState import com.github.damontecres.wholphin.util.LoadingState
import com.github.damontecres.wholphin.util.PlayerFactory
import com.github.damontecres.wholphin.util.TrackActivityPlaybackListener import com.github.damontecres.wholphin.util.TrackActivityPlaybackListener
import com.github.damontecres.wholphin.util.TrackSupport import com.github.damontecres.wholphin.util.TrackSupport
import com.github.damontecres.wholphin.util.checkForSupport import com.github.damontecres.wholphin.util.checkForSupport
@ -98,7 +98,6 @@ class PlaybackViewModel
constructor( constructor(
@param:ApplicationContext val context: Context, @param:ApplicationContext val context: Context,
val api: ApiClient, val api: ApiClient,
val playerFactory: PlayerFactory,
val playlistCreator: PlaylistCreator, val playlistCreator: PlaylistCreator,
val navigationManager: NavigationManager, val navigationManager: NavigationManager,
val itemPlaybackDao: ItemPlaybackDao, val itemPlaybackDao: ItemPlaybackDao,
@ -106,13 +105,13 @@ class PlaybackViewModel
val itemPlaybackRepository: ItemPlaybackRepository, val itemPlaybackRepository: ItemPlaybackRepository,
) : ViewModel(), ) : ViewModel(),
Player.Listener { Player.Listener {
val player by lazy { val player =
playerFactory ExoPlayer
.create(false) .Builder(context)
.build()
.apply { .apply {
playWhenReady = true playWhenReady = true
} }
}
val loading = MutableLiveData<LoadingState>(LoadingState.Loading) val loading = MutableLiveData<LoadingState>(LoadingState.Loading)
@ -732,7 +731,7 @@ class PlaybackViewModel
} }
fun release() { fun release() {
playerFactory.release() player.release()
navigationManager.goBack() navigationManager.goBack()
} }
} }

View file

@ -1,49 +0,0 @@
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
}
}

View file

@ -1,9 +1,17 @@
package com.github.damontecres.wholphin.util package com.github.damontecres.wholphin.util
import android.content.Context
import androidx.annotation.OptIn import androidx.annotation.OptIn
import androidx.media3.common.MediaItem import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi 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 com.github.damontecres.wholphin.preferences.ThemeSongVolume
import dagger.hilt.android.qualifiers.ApplicationContext
import okhttp3.OkHttpClient
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -14,8 +22,11 @@ import javax.inject.Singleton
class ThemeSongPlayer class ThemeSongPlayer
@Inject @Inject
constructor( constructor(
val playerFactory: PlayerFactory, @param:ApplicationContext private val context: Context,
@param:AuthOkHttpClient private val authOkHttpClient: OkHttpClient,
) { ) {
private var player: Player? = null
@OptIn(UnstableApi::class) @OptIn(UnstableApi::class)
fun play( fun play(
volume: ThemeSongVolume, volume: ThemeSongVolume,
@ -35,17 +46,23 @@ class ThemeSongPlayer
ThemeSongVolume.HIGHEST -> 75f ThemeSongVolume.HIGHEST -> 75f
} }
val player = val player =
playerFactory ExoPlayer
.create(true) .Builder(context)
.setMediaSourceFactory(
DefaultMediaSourceFactory(
OkHttpDataSource.Factory(authOkHttpClient),
),
).build()
.apply { .apply {
this.volume = volumeLevel this.volume = volumeLevel
playWhenReady = true playWhenReady = true
} }
player.setMediaItem(MediaItem.fromUri(url)) player.setMediaItem(MediaItem.fromUri(url))
player.prepare() player.prepare()
this.player = player
} }
fun stop() { fun stop() {
playerFactory.release() player?.release()
} }
} }