From 9535bbd43b97d5018c3e49033ae86ed709c97c67 Mon Sep 17 00:00:00 2001 From: Aliaksei Ramanavets Date: Mon, 10 Mar 2025 10:37:24 +0300 Subject: [PATCH 1/3] Fix controller destroying --- .../vr_player/VideoPlayerController.kt | 57 ++++++++++++++----- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/android/src/main/kotlin/wtf/flutter/vr_player/VideoPlayerController.kt b/android/src/main/kotlin/wtf/flutter/vr_player/VideoPlayerController.kt index e44b739..68a9f70 100644 --- a/android/src/main/kotlin/wtf/flutter/vr_player/VideoPlayerController.kt +++ b/android/src/main/kotlin/wtf/flutter/vr_player/VideoPlayerController.kt @@ -90,33 +90,43 @@ class VideoPlayerController( loadMedia(it, result) } } + "seekTo" -> { (methodCall.arguments as? HashMap<*, *>)?.get("position")?.toString() ?.toLongOrNull()?.let { position -> - player?.let { - if (position < it.duration) { - it.seekTo(position) - result.success(methodCall.arguments) + player?.let { + if (position < it.duration) { + it.seekTo(position) + result.success(methodCall.arguments) + } } } - } } + "setVolume" -> { val volume: Float = methodCall.argument("volume")!! player?.setVolume(volume) } + "play" -> { player?.play() result.success(true) } + "pause" -> { player?.pause() result.success(true) } + "isPlaying" -> { result.success(player?.isPlaying == true) } + "dispose" -> { + dispose(false) + result.success(true) + } + "onSizeChanged" -> { if (mediaEntry?.isVRMediaType == true) { reloadPlayer() @@ -127,23 +137,27 @@ class VideoPlayerController( } result.success(true) } + "toggleVRMode" -> { toggleVRMode() } + "fullScreen" -> { if (mediaEntry?.isVRMediaType == true) { - dispose() + dispose(true) } result.success(true) } + "onPause" -> { if (mediaEntry?.isVRMediaType == true) { - dispose() + dispose(false) } else { player?.onApplicationPaused() } result.success(true) } + "onResume" -> { if (mediaEntry?.isVRMediaType == true) { reloadPlayer() @@ -152,12 +166,14 @@ class VideoPlayerController( } result.success(true) } + "onOrientationChanged" -> { if (mediaEntry?.isVRMediaType == true) { - dispose() + dispose(true) } result.success(true) } + else -> result.notImplemented() } } @@ -177,6 +193,7 @@ class VideoPlayerController( isVRModeEnabled = !isVRModeEnabled } } + private fun loadMedia(videoUrl: HashMap<*, *>, result: MethodChannel.Result?) { this.videoUrl = videoUrl this.mediaEntry = createMediaEntry(videoUrl) @@ -229,16 +246,16 @@ class VideoPlayerController( // Set the format of the source. // In our case it will be hls // in case of mpd/wvm formats you have to to call mediaSource.setDrmData method as well - if (videoUrl.get("videoPath") != null){ + if (videoUrl.get("videoPath") != null) { val url = videoUrl.get("videoPath") as String val extension = url.substringAfterLast('.', ""); mediaSource.url = url - mediaSource.mediaFormat = if(extension=="mp4") PKMediaFormat.mp4 else PKMediaFormat.hls + mediaSource.mediaFormat = if (extension == "mp4") PKMediaFormat.mp4 else PKMediaFormat.hls } else { val url = videoUrl.get("videoUrl") as String val extension = url.substringAfterLast('.', ""); mediaSource.url = url - mediaSource.mediaFormat = if(extension=="mp4") PKMediaFormat.mp4 else PKMediaFormat.hls + mediaSource.mediaFormat = if (extension == "mp4") PKMediaFormat.mp4 else PKMediaFormat.hls } return listOf(mediaSource) @@ -288,12 +305,15 @@ class VideoPlayerController( } playerEventStateChanged?.success(mapOf(Pair("state", 1))) } + PlayerState.LOADING -> { playerEventStateChanged?.success(mapOf(Pair("state", 0))) } + PlayerState.BUFFERING -> { playerEventStateChanged?.success(mapOf(Pair("state", 2))) } + else -> {} } } @@ -327,13 +347,22 @@ class VideoPlayerController( return vrSettings } - fun dispose() { + fun dispose(isRebuilding: Boolean = false) { player?.let { this.videoPlayerState = VideoPlayerState(it.currentPosition, it.isPlaying) - it.destroy() + + if (!isRebuilding) { + it.destroy() + player = null + } } - player = null + + playerEventStateChanged = null + playerEventDurationChanged = null + playerEventPosition = null + playerEventEnded = null } + inner class VideoPlayerState(val currentPosition: Long = 0, val isPlaying: Boolean = false) } \ No newline at end of file From 992e7ed2ff93d7939102a367a86a047eb3f09cca Mon Sep 17 00:00:00 2001 From: Aliaksei Ramanavets Date: Mon, 10 Mar 2025 12:11:08 +0300 Subject: [PATCH 2/3] Resolve conflict --- .../flutter/vr_player/VideoPlayerController.kt | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/android/src/main/kotlin/wtf/flutter/vr_player/VideoPlayerController.kt b/android/src/main/kotlin/wtf/flutter/vr_player/VideoPlayerController.kt index 7decdf0..b3e043e 100644 --- a/android/src/main/kotlin/wtf/flutter/vr_player/VideoPlayerController.kt +++ b/android/src/main/kotlin/wtf/flutter/vr_player/VideoPlayerController.kt @@ -117,7 +117,7 @@ class VideoPlayerController( result.success(player?.isPlaying == true) } "dispose" -> { - dispose() + dispose(false) result.success(true) } "onSizeChanged" -> { @@ -135,13 +135,13 @@ class VideoPlayerController( } "fullScreen" -> { if (mediaEntry?.isVRMediaType == true) { - dispose() + dispose(true) } result.success(true) } "onPause" -> { if (mediaEntry?.isVRMediaType == true) { - dispose() + dispose(false) } else { player?.onApplicationPaused() } @@ -157,7 +157,7 @@ class VideoPlayerController( } "onOrientationChanged" -> { if (mediaEntry?.isVRMediaType == true) { - dispose() + dispose(true) } result.success(true) } @@ -330,12 +330,15 @@ class VideoPlayerController( return vrSettings } - fun dispose() { + fun dispose(isRebuilding: Boolean = false) { player?.let { this.videoPlayerState = VideoPlayerState(it.currentPosition, it.isPlaying) - it.destroy() + + if (!isRebuilding) { + it.destroy() + player = null + } } - player = null playerEventStateChanged = null playerEventDurationChanged = null From 98e30560f7e795106236d4b1273409854127d107 Mon Sep 17 00:00:00 2001 From: Aliaksei Ramanavets Date: Mon, 10 Mar 2025 12:17:29 +0300 Subject: [PATCH 3/3] Change line length --- lib/src/vr_player.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/src/vr_player.dart b/lib/src/vr_player.dart index 9461f4c..2052af4 100644 --- a/lib/src/vr_player.dart +++ b/lib/src/vr_player.dart @@ -48,7 +48,8 @@ class _VideoPlayerState extends State with WidgetsBindingObserver { void didUpdateWidget(VrPlayer oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.width != widget.width) { - final pixelRatio = Platform.isAndroid ? MediaQuery.of(context).devicePixelRatio : 1; + final pixelRatio = + Platform.isAndroid ? MediaQuery.of(context).devicePixelRatio : 1; final width = widget.width * pixelRatio; final height = widget.height * pixelRatio; @@ -100,7 +101,8 @@ class _VideoPlayerState extends State with WidgetsBindingObserver { return AndroidViewSurface( controller: controller as AndroidViewController, hitTestBehavior: PlatformViewHitTestBehavior.opaque, - gestureRecognizers: const >{ + gestureRecognizers: const >{ Factory(TapGestureRecognizer.new), }, );