implement player callback
This commit is contained in:
parent
70d7f55cf6
commit
03ac3b5f16
|
@ -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)
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
package net.newpipe.newplayer.model
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import androidx.media3.common.MediaItem
|
||||
import androidx.media3.common.Player
|
||||
|
@ -29,18 +29,19 @@ import dagger.hilt.android.lifecycle.HiltViewModel
|
|||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import net.newpipe.newplayer.R
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
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,
|
||||
var fullscreen: Boolean,
|
||||
var uiVissible: Boolean
|
||||
){
|
||||
) {
|
||||
companion object {
|
||||
val DEFAULT = VideoPlayerUIState(
|
||||
playing = true,
|
||||
playing = false,
|
||||
fullscreen = false,
|
||||
uiVissible = false
|
||||
)
|
||||
|
@ -50,15 +51,20 @@ 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(
|
||||
private val savedStateHandle: SavedStateHandle,
|
||||
|
@ -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.addListener(object : Player.Listener {
|
||||
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
||||
super.onIsPlayingChanged(isPlaying)
|
||||
println("gurken playerstate: $isPlaying")
|
||||
mutableUiState.update {
|
||||
it.copy(playing = 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()
|
||||
mutableUiState.update {
|
||||
it.copy(playing = player.isPlaying)
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue