Skip to content

Commit

Permalink
update: DanmakuVideoPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
muedsa committed Nov 7, 2023
1 parent b4712f5 commit 4f242a0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ import androidx.media3.common.MediaItem
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.datasource.DefaultDataSource
import androidx.media3.datasource.DefaultHttpDataSource
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
import androidx.media3.extractor.DefaultExtractorsFactory
import com.muedsa.agetv.BuildConfig
import com.muedsa.compose.tv.widget.ErrorMessageBox
import com.muedsa.compose.tv.widget.ErrorMessageBoxState
import com.muedsa.compose.tv.widget.player.SimpleVideoPlayer
import com.muedsa.uitl.ChromeUserAgent
import com.muedsa.uitl.LogUtil

@Composable
Expand All @@ -32,40 +27,19 @@ fun PlaybackScreen(
val errorMessageBoxState = remember { ErrorMessageBoxState() }
ErrorMessageBox(state = errorMessageBoxState) {
SimpleVideoPlayer(
debug = BuildConfig.DEBUG,
playerBuilderInit = {
val dataSourceFactory = DefaultDataSource.Factory(
context,
DefaultHttpDataSource.Factory()
.setDefaultRequestProperties(
mapOf(
"Sec-Ch-Ua" to "\"Chromium\";v=\"118\", \"Google Chrome\";v=\"118\", \"Not=A?Brand\";v=\"99\"",
"Sec-Ch-Ua-Mobile" to "?0",
"Sec-Ch-Ua-Platform" to "\"Windows\""
)
)
.setUserAgent(ChromeUserAgent)
)
setMediaSourceFactory(
DefaultMediaSourceFactory(
dataSourceFactory,
DefaultExtractorsFactory()
)
)
},
playerInit = {
addListener(object : Player.Listener {
override fun onPlayerErrorChanged(error: PlaybackException?) {
errorMessageBoxState.error(error, SnackbarDuration.Long)
error?.let {
LogUtil.fb(it, "exoplayer mediaUrl: $mediaUrl")
}
debug = BuildConfig.DEBUG
) {
addListener(object : Player.Listener {
override fun onPlayerErrorChanged(error: PlaybackException?) {
errorMessageBoxState.error(error, SnackbarDuration.Long)
error?.let {
LogUtil.fb(it, "exoplayer mediaUrl: $mediaUrl")
}
})
playWhenReady = true
setMediaItem(MediaItem.fromUri(mediaUrl))
prepare()
}
)
}
})
playWhenReady = true
setMediaItem(MediaItem.fromUri(mediaUrl))
prepare()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import kotlin.time.toDuration
@Composable
fun DanmakuVideoPlayer(
debug: Boolean = false,
videoPlayerBuilderSetting: ExoPlayer.Builder.() -> Unit = {},
videoPlayerInit: ExoPlayer.() -> Unit,
danmakuPlayerInit: DanmakuPlayer.() -> Unit
) {
Expand All @@ -89,6 +90,7 @@ fun DanmakuVideoPlayer(

val exoPlayer = remember {
ExoPlayer.Builder(context)
.also(videoPlayerBuilderSetting)
.build()
.also {
if (debug) {
Expand Down Expand Up @@ -155,7 +157,7 @@ fun DanmakuVideoPlayer(
@Composable
fun SimpleVideoPlayer(
debug: Boolean = false,
playerBuilderInit: ExoPlayer.Builder.() -> Unit = {},
playerBuilderSetting: ExoPlayer.Builder.() -> Unit = {},
playerInit: ExoPlayer.() -> Unit
) {
val context = LocalContext.current
Expand All @@ -164,7 +166,7 @@ fun SimpleVideoPlayer(

val exoPlayer = remember {
ExoPlayer.Builder(context)
.also(playerBuilderInit)
.also(playerBuilderSetting)
.build()
.also {
if (debug) {
Expand Down

0 comments on commit 4f242a0

Please sign in to comment.