Fixed destuctor bug

This commit is contained in:
Alex Kim
2024-11-19 02:55:01 +11:00
parent 72410d2729
commit f127ee2976

View File

@@ -25,6 +25,7 @@ class VlcPlayerView(context: Context, appContext: AppContext) : ExpoView(context
private var lastReportedState: Int? = null private var lastReportedState: Int? = null
private var lastReportedIsPlaying: Boolean? = null private var lastReportedIsPlaying: Boolean? = null
private var startPosition: Int? = null private var startPosition: Int? = null
private var media : Media? = null
private val onVideoProgress by EventDispatcher() private val onVideoProgress by EventDispatcher()
private val onVideoStateChange by EventDispatcher() private val onVideoStateChange by EventDispatcher()
@@ -52,6 +53,7 @@ class VlcPlayerView(context: Context, appContext: AppContext) : ExpoView(context
val isNetwork = source["isNetwork"] as? Boolean ?: false val isNetwork = source["isNetwork"] as? Boolean ?: false
startPosition = (source["startPosition"] as? Double)?.toInt() ?: 0 startPosition = (source["startPosition"] as? Double)?.toInt() ?: 0
println("startPosition $startPosition")
// Handle video load start event // Handle video load start event
// onVideoLoadStart?.invoke(mapOf("target" to reactTag ?: "null")) // onVideoLoadStart?.invoke(mapOf("target" to reactTag ?: "null"))
@@ -61,7 +63,7 @@ class VlcPlayerView(context: Context, appContext: AppContext) : ExpoView(context
mediaPlayer?.setEventListener(this) mediaPlayer?.setEventListener(this)
Log.d("VlcPlayerView", "Loading network file: $uri") Log.d("VlcPlayerView", "Loading network file: $uri")
val media = Media(libVLC, Uri.parse(uri)) media = Media(libVLC, Uri.parse(uri))
mediaPlayer?.media = media mediaPlayer?.media = media
@@ -146,11 +148,10 @@ class VlcPlayerView(context: Context, appContext: AppContext) : ExpoView(context
mediaPlayer?.addSlave(IMedia.Slave.Type.Subtitle, Uri.parse(subtitleURL), true) mediaPlayer?.addSlave(IMedia.Slave.Type.Subtitle, Uri.parse(subtitleURL), true)
} }
// Kotlin has its own garbage collector.
override fun onDetachedFromWindow() { override fun onDetachedFromWindow() {
super.onDetachedFromWindow() println("onDetachedFromWindow")
mediaPlayer?.release()
mediaPlayer = null
libVLC?.release()
} }
override fun onEvent(event: MediaPlayer.Event) { override fun onEvent(event: MediaPlayer.Event) {
@@ -219,6 +220,8 @@ class VlcPlayerView(context: Context, appContext: AppContext) : ExpoView(context
val player = mediaPlayer ?: return val player = mediaPlayer ?: return
val startPosition = startPosition ?: return val startPosition = startPosition ?: return
println("seekToStartTime $startPosition")
if (startPosition > 0) { if (startPosition > 0) {
Log.d("VlcPlayerView", "Debug: Seeking to start position: $startPosition") Log.d("VlcPlayerView", "Debug: Seeking to start position: $startPosition")
player.time = startPosition.toLong() player.time = startPosition.toLong()
@@ -236,6 +239,8 @@ class VlcPlayerView(context: Context, appContext: AppContext) : ExpoView(context
val currentTimeMs = player.time.toInt() val currentTimeMs = player.time.toInt()
val durationMs = player.media?.duration?.toInt() ?: 0 val durationMs = player.media?.duration?.toInt() ?: 0
println("currentTimeMs $currentTimeMs")
if (currentTimeMs >= 0 && currentTimeMs < durationMs) { if (currentTimeMs >= 0 && currentTimeMs < durationMs) {
onVideoProgress(mapOf( onVideoProgress(mapOf(
"currentTime" to currentTimeMs, "currentTime" to currentTimeMs,