make fast seek foo work

This commit is contained in:
Christian Schabesberger 2024-08-07 18:57:14 +02:00
parent 545f9d6650
commit a5c9baa26d
4 changed files with 24 additions and 28 deletions

View File

@ -52,8 +52,6 @@ interface NewPlayer {
fun prepare()
fun play()
fun pause()
fun fastSeek(steps: Int)
fun seekTo(millisecond: Long)
fun addToPlaylist(newItem: String)
fun addListener(callbackListener: Listener)
@ -85,9 +83,15 @@ class NewPlayerImpl(override val internal_player: Player, override val repositor
private var callbackListeners: MutableList<NewPlayer.Listener> = ArrayList()
override val duartion: Long = internal_player.duration
override val bufferedPercentage: Int = internal_player.bufferedPercentage
override var currentPosition: Long = internal_player.currentPosition
override val duartion: Long
get() = internal_player.duration
override val bufferedPercentage: Int
get() = internal_player.bufferedPercentage
override var currentPosition: Long
get() = internal_player.currentPosition
set(value) {internal_player.seekTo(value)}
override var fastSeekAmountSec: Int = 10
override var playBackMode: PlayMode = PlayMode.EMBEDDED_VIDEO
override var playList: MutableList<String> = ArrayList<String>()
@ -114,15 +118,6 @@ class NewPlayerImpl(override val internal_player: Player, override val repositor
internal_player.pause()
}
override fun fastSeek(steps: Int) {
val currentPosition = internal_player.currentPosition
internal_player.seekTo(currentPosition + fastSeekAmountSec * 1000 * steps)
}
override fun seekTo(millisecond: Long) {
internal_player.seekTo(millisecond)
}
override fun addToPlaylist(newItem: String) {
Log.d(TAG, "Not implemented add to playlist")
}

View File

@ -29,7 +29,7 @@ import net.newpipe.newplayer.ui.ContentScale
interface VideoPlayerViewModel {
var newPlayer: NewPlayer?
val player: Player?
val internalPlayer: Player?
val uiState: StateFlow<VideoPlayerUIState>
var minContentRatio: Float
var maxContentRatio: Float

View File

@ -65,12 +65,11 @@ class VideoPlayerViewModelImpl @Inject constructor(
set(value) {
field = value
installExoPlayer()
mutableUiState.update { it.copy(fastseekSeconds = field?.fastSeekAmountSec ?: 10) }
}
override val uiState = mutableUiState.asStateFlow()
override val player: Player?
override val internalPlayer: Player?
get() = newPlayer?.internal_player
override var minContentRatio: Float = 4F / 3F
@ -109,7 +108,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
}
private fun installExoPlayer() {
player?.let { player ->
internalPlayer?.let { player ->
Log.d(TAG, "Install player: ${player.videoSize.width}")
player.addListener(object : Player.Listener {
@ -228,9 +227,9 @@ class VideoPlayerViewModelImpl @Inject constructor(
}
private fun updateProgressOnce() {
val progress = player?.currentPosition ?: 0
val duration = player?.duration ?: 1
val bufferedPercentage = (player?.bufferedPercentage?.toFloat() ?: 0f) / 100f
val progress = internalPlayer?.currentPosition ?: 0
val duration = internalPlayer?.duration ?: 1
val bufferedPercentage = (internalPlayer?.bufferedPercentage?.toFloat() ?: 0f) / 100f
val progressPercentage = progress.toFloat() / duration.toFloat()
mutableUiState.update {
@ -259,8 +258,8 @@ class VideoPlayerViewModelImpl @Inject constructor(
override fun seekingFinished() {
resetHideUiDelayedJob()
val seekerPosition = mutableUiState.value.seekerPosition
val seekPositionInMs = (player?.duration?.toFloat() ?: 0F) * seekerPosition
newPlayer?.seekTo(seekPositionInMs.toLong())
val seekPositionInMs = (internalPlayer?.duration?.toFloat() ?: 0F) * seekerPosition
newPlayer?.currentPosition = seekPositionInMs.toLong()
Log.i(TAG, "Seek to Ms: $seekPositionInMs")
}
@ -282,7 +281,9 @@ class VideoPlayerViewModelImpl @Inject constructor(
override fun fastSeekFinished() {
val fastSeekAmount = mutableUiState.value.fastseekSeconds
newPlayer?.fastSeek(fastSeekAmount)
Log.d(TAG, "$fastSeekAmount")
newPlayer?.currentPosition = (newPlayer?.currentPosition ?: 0) + (fastSeekAmount * 1000)
mutableUiState.update {
it.copy(fastseekSeconds = 0)
}
@ -306,7 +307,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
}
private fun getEmbeddedUiRatio() =
player?.let { player ->
internalPlayer?.let { player ->
val videoRatio = VideoSize.fromMedia3VideoSize(player.videoSize).getRatio()
return (if (videoRatio.isNaN())
currentContentRatio
@ -320,7 +321,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
companion object {
val dummy = object : VideoPlayerViewModel {
override var newPlayer: NewPlayer? = null
override val player: Player? = null
override val internalPlayer: Player? = null
override val uiState = MutableStateFlow(VideoPlayerUIState.DEFAULT)
override var minContentRatio = 4F / 3F
override var maxContentRatio = 16F / 9F

View File

@ -62,7 +62,7 @@ fun VideoPlayerUI(
) {
if (viewModel == null) {
VideoPlayerLoadingPlaceholder()
} else if (viewModel.player == null) {
} else if (viewModel.internalPlayer == null) {
VideoPlayerLoadingPlaceholder(viewModel.uiState.collectAsState().value.embeddedUiRatio)
} else {
val uiState by viewModel.uiState.collectAsState()
@ -136,7 +136,7 @@ fun VideoPlayerUI(
) {
Box(contentAlignment = Alignment.Center) {
PlaySurface(
player = viewModel.player,
player = viewModel.internalPlayer,
lifecycle = lifecycle,
fitMode = uiState.contentFitMode,
uiRatio = if (uiState.fullscreen) screenRatio else uiState.embeddedUiRatio,