mirror of
https://github.com/JustinZeus/Wholphin.git
synced 2026-07-08 15:50:16 +02:00
Fix playback issues from c3cc284
This commit is contained in:
parent
a31cdc79e1
commit
95a74b73ff
3 changed files with 27 additions and 60 deletions
|
|
@ -15,6 +15,7 @@ import androidx.media3.common.Player
|
|||
import androidx.media3.common.TrackSelectionOverride
|
||||
import androidx.media3.common.Tracks
|
||||
import androidx.media3.common.util.UnstableApi
|
||||
import androidx.media3.exoplayer.ExoPlayer
|
||||
import com.github.damontecres.wholphin.data.ItemPlaybackDao
|
||||
import com.github.damontecres.wholphin.data.ItemPlaybackRepository
|
||||
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.LoadingExceptionHandler
|
||||
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.TrackSupport
|
||||
import com.github.damontecres.wholphin.util.checkForSupport
|
||||
|
|
@ -98,7 +98,6 @@ class PlaybackViewModel
|
|||
constructor(
|
||||
@param:ApplicationContext val context: Context,
|
||||
val api: ApiClient,
|
||||
val playerFactory: PlayerFactory,
|
||||
val playlistCreator: PlaylistCreator,
|
||||
val navigationManager: NavigationManager,
|
||||
val itemPlaybackDao: ItemPlaybackDao,
|
||||
|
|
@ -106,13 +105,13 @@ class PlaybackViewModel
|
|||
val itemPlaybackRepository: ItemPlaybackRepository,
|
||||
) : ViewModel(),
|
||||
Player.Listener {
|
||||
val player by lazy {
|
||||
playerFactory
|
||||
.create(false)
|
||||
val player =
|
||||
ExoPlayer
|
||||
.Builder(context)
|
||||
.build()
|
||||
.apply {
|
||||
playWhenReady = true
|
||||
}
|
||||
}
|
||||
|
||||
val loading = MutableLiveData<LoadingState>(LoadingState.Loading)
|
||||
|
||||
|
|
@ -732,7 +731,7 @@ class PlaybackViewModel
|
|||
}
|
||||
|
||||
fun release() {
|
||||
playerFactory.release()
|
||||
player.release()
|
||||
navigationManager.goBack()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,17 @@
|
|||
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
|
||||
|
||||
|
|
@ -14,8 +22,11 @@ import javax.inject.Singleton
|
|||
class ThemeSongPlayer
|
||||
@Inject
|
||||
constructor(
|
||||
val playerFactory: PlayerFactory,
|
||||
@param:ApplicationContext private val context: Context,
|
||||
@param:AuthOkHttpClient private val authOkHttpClient: OkHttpClient,
|
||||
) {
|
||||
private var player: Player? = null
|
||||
|
||||
@OptIn(UnstableApi::class)
|
||||
fun play(
|
||||
volume: ThemeSongVolume,
|
||||
|
|
@ -35,17 +46,23 @@ class ThemeSongPlayer
|
|||
ThemeSongVolume.HIGHEST -> 75f
|
||||
}
|
||||
val player =
|
||||
playerFactory
|
||||
.create(true)
|
||||
ExoPlayer
|
||||
.Builder(context)
|
||||
.setMediaSourceFactory(
|
||||
DefaultMediaSourceFactory(
|
||||
OkHttpDataSource.Factory(authOkHttpClient),
|
||||
),
|
||||
).build()
|
||||
.apply {
|
||||
this.volume = volumeLevel
|
||||
playWhenReady = true
|
||||
}
|
||||
player.setMediaItem(MediaItem.fromUri(url))
|
||||
player.prepare()
|
||||
this.player = player
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
playerFactory.release()
|
||||
player?.release()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue