implement player callback

This commit is contained in:
Christian Schabesberger 2024-07-10 17:59:55 +02:00
parent 70d7f55cf6
commit 03ac3b5f16
4 changed files with 87 additions and 25 deletions

View File

@ -46,6 +46,12 @@ class PlayerFragment() : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
viewModel.listener = object:VideoPlayerViewModel.Listener{
override fun contentRatioChagned(ratio: Float) {
println("gurken ratio is: $ratio")
}
}
val view = inflater.inflate(R.layout.player_framgent, container, false)
val composeView = view.findViewById<ComposeView>(R.id.player_copose_view)

View File

@ -32,6 +32,7 @@ import javax.inject.Inject
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import net.newpipe.newplayer.utils.VideoSize
data class VideoPlayerUIState(
val playing: Boolean,
@ -40,7 +41,7 @@ data class VideoPlayerUIState(
) {
companion object {
val DEFAULT = VideoPlayerUIState(
playing = true,
playing = false,
fullscreen = false,
uiVissible = false
)
@ -50,14 +51,19 @@ data class VideoPlayerUIState(
interface VideoPlayerViewModel {
val player: Player?
val uiState: StateFlow<VideoPlayerUIState>
var listener: Listener?
fun play()
fun pause()
fun uiResume()
fun prevStream()
fun nextStream()
fun switchToFullscreen()
fun switchToEmbeddedView()
interface Listener {
fun contentRatioChagned(ratio: Float)
}
}
@HiltViewModel
class VideoPlayerViewModelImpl @Inject constructor(
@ -74,28 +80,54 @@ class VideoPlayerViewModelImpl @Inject constructor(
override val uiState = mutableUiState.asStateFlow()
override var listener: VideoPlayerViewModel.Listener? = null
var current_video_size = VideoSize.DEFAULT
init {
player.prepare()
player.setMediaItem(MediaItem.fromUri(app.getString(R.string.ccc_6502_video)))
player.addListener(object : Player.Listener {
override fun onIsPlayingChanged(isPlaying: Boolean) {
super.onIsPlayingChanged(isPlaying)
println("gurken playerstate: $isPlaying")
mutableUiState.update {
it.copy(playing = isPlaying)
}
}
override fun play() {
player.play()
mutableUiState.update {
it.copy(playing = player.isPlaying)
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
super.onMediaItemTransition(mediaItem, reason)
println("gurken mediaitem transition")
val videoSize = VideoSize.fromMedia3VideoSize(player.videoSize)
val hight = player.videoSize.height
val width = player.videoSize.width
println("gurken videoSize: $videoSize, currentSize: $width, $hight")
TODO("DEN DIRNENSOHN FIXEN")
if(current_video_size != videoSize) {
if(current_video_size.getRatio() != videoSize.getRatio()) {
listener?.contentRatioChagned(videoSize.getRatio())
}
current_video_size = videoSize
}
}
})
player.setMediaItem(MediaItem.fromUri(app.getString(R.string.ccc_6502_video)))
player.playWhenReady = true
}
override fun play() {
println("gurken Play")
player.play()
}
override fun pause() {
println("gurken pause")
player.pause()
mutableUiState.update {
it.copy(playing = player.isPlaying)
}
}
override fun uiResume() {
play()
}
override fun prevStream() {
@ -126,6 +158,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
val dummy = object : VideoPlayerViewModel {
override val player = null
override val uiState = MutableStateFlow(VideoPlayerUIState.DEFAULT)
override var listener: VideoPlayerViewModel.Listener? = null
override fun play() {
println("dummy impl")
}
@ -142,10 +175,6 @@ class VideoPlayerViewModelImpl @Inject constructor(
println("dummy pause")
}
override fun uiResume() {
println("dummy ui resume")
}
override fun prevStream() {
println("dummy impl")
}

View File

@ -78,13 +78,10 @@ fun VideoPlayerUI(
}, update = {
when (lifecycle) {
Lifecycle.Event.ON_PAUSE -> {
println("gurken state on pause")
viewModel.pause()
}
Lifecycle.Event.ON_RESUME -> {
viewModel.uiResume()
}
else -> Unit
}
})
@ -92,7 +89,7 @@ fun VideoPlayerUI(
val isPlaying = viewModel.player!!.isPlaying
println("is Player playing: $isPlaying")
VideoPlayerControllerUI(
isPlaying = viewModel.player?.isPlaying ?: false,
isPlaying = uiState.playing,
isFullscreen = uiState.fullscreen,
play = viewModel::play,
pause = viewModel::pause,

View File

@ -0,0 +1,30 @@
package net.newpipe.newplayer.utils
data class VideoSize(
val width: Int,
val height: Int,
/// The width/height ratio of a single pixel
val pixelWidthHeightRatio: Float
) {
override fun equals(other: Any?) =
when (other) {
is VideoSize ->
other.width == this.width && other.height == this.height && pixelWidthHeightRatio == other.pixelWidthHeightRatio
else -> false
}
override fun hashCode() =
width + height * 9999999 + (pixelWidthHeightRatio*10000).toInt()
fun getRatio() =
(width * pixelWidthHeightRatio) / height
companion object {
val DEFAULT = VideoSize(0, 0, 1F)
fun fromMedia3VideoSize(videoSize: androidx.media3.common.VideoSize) =
VideoSize(videoSize.width, videoSize.height, videoSize.pixelWidthHeightRatio)
}
}