mirror of
https://github.com/JustinZeus/Wholphin.git
synced 2026-07-09 16:11:20 +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
|
|
@ -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