mirror of
https://github.com/gabehf/Fladder.git
synced 2026-03-07 21:48:14 -08:00
fix: Incorrect progress updating for native player
This commit is contained in:
parent
fa61ce2e40
commit
a3ccb6009c
6 changed files with 57 additions and 32 deletions
|
|
@ -714,8 +714,8 @@ interface NativeVideoActivity {
|
|||
}
|
||||
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
|
||||
interface VideoPlayerApi {
|
||||
fun sendPlayableModel(playableData: PlayableData): Boolean
|
||||
fun open(url: String, play: Boolean)
|
||||
fun sendPlayableModel(playableData: PlayableData, callback: (Result<Boolean>) -> Unit)
|
||||
fun open(url: String, play: Boolean, callback: (Result<Boolean>) -> Unit)
|
||||
fun setLooping(looping: Boolean)
|
||||
/** Sets the volume, with 0.0 being muted and 1.0 being full volume. */
|
||||
fun setVolume(volume: Double)
|
||||
|
|
@ -743,12 +743,15 @@ interface VideoPlayerApi {
|
|||
channel.setMessageHandler { message, reply ->
|
||||
val args = message as List<Any?>
|
||||
val playableDataArg = args[0] as PlayableData
|
||||
val wrapped: List<Any?> = try {
|
||||
listOf(api.sendPlayableModel(playableDataArg))
|
||||
} catch (exception: Throwable) {
|
||||
VideoPlayerHelperPigeonUtils.wrapError(exception)
|
||||
api.sendPlayableModel(playableDataArg) { result: Result<Boolean> ->
|
||||
val error = result.exceptionOrNull()
|
||||
if (error != null) {
|
||||
reply.reply(VideoPlayerHelperPigeonUtils.wrapError(error))
|
||||
} else {
|
||||
val data = result.getOrNull()
|
||||
reply.reply(VideoPlayerHelperPigeonUtils.wrapResult(data))
|
||||
}
|
||||
}
|
||||
reply.reply(wrapped)
|
||||
}
|
||||
} else {
|
||||
channel.setMessageHandler(null)
|
||||
|
|
@ -761,13 +764,15 @@ interface VideoPlayerApi {
|
|||
val args = message as List<Any?>
|
||||
val urlArg = args[0] as String
|
||||
val playArg = args[1] as Boolean
|
||||
val wrapped: List<Any?> = try {
|
||||
api.open(urlArg, playArg)
|
||||
listOf(null)
|
||||
} catch (exception: Throwable) {
|
||||
VideoPlayerHelperPigeonUtils.wrapError(exception)
|
||||
api.open(urlArg, playArg) { result: Result<Boolean> ->
|
||||
val error = result.exceptionOrNull()
|
||||
if (error != null) {
|
||||
reply.reply(VideoPlayerHelperPigeonUtils.wrapError(error))
|
||||
} else {
|
||||
val data = result.getOrNull()
|
||||
reply.reply(VideoPlayerHelperPigeonUtils.wrapResult(data))
|
||||
}
|
||||
}
|
||||
reply.reply(wrapped)
|
||||
}
|
||||
} else {
|
||||
channel.setMessageHandler(null)
|
||||
|
|
|
|||
|
|
@ -26,18 +26,23 @@ class VideoPlayerImplementation(
|
|||
var player: ExoPlayer? = null
|
||||
val playbackData: MutableStateFlow<PlayableData?> = MutableStateFlow(null)
|
||||
|
||||
override fun sendPlayableModel(playableData: PlayableData): Boolean {
|
||||
override fun sendPlayableModel(
|
||||
playableData: PlayableData,
|
||||
callback: (Result<Boolean>) -> Unit
|
||||
) {
|
||||
try {
|
||||
println("Send playable data")
|
||||
playbackData.value = playableData
|
||||
return true
|
||||
callback(Result.success(true))
|
||||
return
|
||||
} catch (e: Exception) {
|
||||
println("Error loading data $e")
|
||||
return false
|
||||
callback(Result.success(false))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
override fun open(url: String, play: Boolean) {
|
||||
override fun open(url: String, play: Boolean, callback: (Result<Boolean>) -> Unit) {
|
||||
Handler(Looper.getMainLooper()).postDelayed(delayInMillis = 1.seconds.inWholeMilliseconds) {
|
||||
try {
|
||||
playbackData.value?.let {
|
||||
|
|
@ -67,17 +72,21 @@ class VideoPlayerImplementation(
|
|||
player?.prepare()
|
||||
|
||||
val startPosition = playbackData.value?.startPosition ?: 0L
|
||||
if (startPosition > 0) {
|
||||
if (startPosition > 0L) {
|
||||
player?.seekTo(startPosition)
|
||||
|
||||
player?.playWhenReady = play
|
||||
}
|
||||
player?.playWhenReady = play
|
||||
callback(Result.success(true))
|
||||
return@postDelayed
|
||||
} catch (e: Exception) {
|
||||
println("Error playing video $e")
|
||||
callback(Result.success(false))
|
||||
return@postDelayed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override fun setLooping(looping: Boolean) {
|
||||
player?.repeatMode = if (looping) Player.REPEAT_MODE_ONE else Player.REPEAT_MODE_OFF
|
||||
}
|
||||
|
|
@ -110,10 +119,9 @@ class VideoPlayerImplementation(
|
|||
player = exoPlayer
|
||||
//exoPlayer initializes after the playbackData is set for the first load
|
||||
playbackData.value?.let {
|
||||
sendPlayableModel(it)
|
||||
VideoPlayerObject.setAudioTrackIndex(it.defaultAudioTrack.toInt(), true)
|
||||
VideoPlayerObject.setSubtitleTrackIndex(it.defaultSubtrack.toInt(), true)
|
||||
open(it.url, true)
|
||||
open(it.url, true, callback = {})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue