From 9a16b68bc430bf4a4263ceee3dfd2f1e376fe7c5 Mon Sep 17 00:00:00 2001 From: Dot Cink Date: Thu, 5 Mar 2020 20:12:35 +0800 Subject: [PATCH 1/3] fix #203: set `prepared` status after it has **actually** been prepared for Android --- .../src/main/java/top/kikt/ijkplayer/Ijk.kt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/top/kikt/ijkplayer/Ijk.kt b/android/src/main/java/top/kikt/ijkplayer/Ijk.kt index 51371fe..c1c03a8 100755 --- a/android/src/main/java/top/kikt/ijkplayer/Ijk.kt +++ b/android/src/main/java/top/kikt/ijkplayer/Ijk.kt @@ -7,6 +7,8 @@ import android.content.Context import android.graphics.Bitmap import android.media.AudioManager import android.net.Uri +import android.os.Handler +import android.os.Looper import android.util.Base64 import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.PluginRegistry @@ -16,6 +18,7 @@ import tv.danmaku.ijk.media.player.IjkMediaPlayer import tv.danmaku.ijk.media.player.TextureMediaPlayer import java.io.ByteArrayOutputStream import java.io.File +import java.lang.RuntimeException class Ijk(private val registry: PluginRegistry.Registrar, private val options: Map) { @@ -30,6 +33,8 @@ class Ijk(private val registry: PluginRegistry.Registrar, private val options: M private val notifyChannel: NotifyChannel = NotifyChannel(registry, id, this) + private val handler = Handler(Looper.getMainLooper()) + var degree = 0 var isDisposed = false @@ -230,8 +235,19 @@ class Ijk(private val registry: PluginRegistry.Registrar, private val options: M mediaPlayer.setDataSource(appContext, uri, headers) // } mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC) + + val timeoutRunnable = Runnable { + // to avoid 'Reply already submitted' exception + mediaPlayer.setOnPreparedListener(null) + callback(RuntimeException("Prepare timeout")) + } + handler.postDelayed(timeoutRunnable, 15 * 1000) + mediaPlayer.setOnPreparedListener { + handler.removeCallbacks(timeoutRunnable) + callback(null) + } + mediaPlayer.prepareAsync() - callback(null) } catch (e: Exception) { e.printStackTrace() callback(e) From eed08cb09cf565a2a4e8d700ecb45935f2c58393 Mon Sep 17 00:00:00 2001 From: Dot Cink Date: Thu, 5 Mar 2020 20:13:07 +0800 Subject: [PATCH 2/3] fix: flutter does not recognize Throwable error --- android/src/main/java/top/kikt/ijkplayer/Ijk.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/top/kikt/ijkplayer/Ijk.kt b/android/src/main/java/top/kikt/ijkplayer/Ijk.kt index c1c03a8..74a1078 100755 --- a/android/src/main/java/top/kikt/ijkplayer/Ijk.kt +++ b/android/src/main/java/top/kikt/ijkplayer/Ijk.kt @@ -219,7 +219,7 @@ class Ijk(private val registry: PluginRegistry.Registrar, private val options: M result?.success(true) } else { throwable.printStackTrace() - result?.error("1", "set resource error", throwable) + result?.error("1", "set resource error", throwable.toString()) } } From 79478dd1a622e1804123eb35375d8d6ef124be39 Mon Sep 17 00:00:00 2001 From: Dot Cink Date: Thu, 5 Mar 2020 20:13:16 +0800 Subject: [PATCH 3/3] set setDatasourceFail status on error --- lib/src/controller/controller.dart | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/src/controller/controller.dart b/lib/src/controller/controller.dart index 3856e41..c2d21b9 100755 --- a/lib/src/controller/controller.dart +++ b/lib/src/controller/controller.dart @@ -97,12 +97,16 @@ class IjkMediaController bool autoPlay = false, }) async { _ijkStatus = IjkStatus.preparing; - await _initDataSource(autoPlay); - await _plugin?.setNetworkDataSource( - uri: url, - headers: headers, - ); - _ijkStatus = IjkStatus.prepared; + try { + await _initDataSource(autoPlay); + await _plugin?.setNetworkDataSource( + uri: url, + headers: headers, + ); + _ijkStatus = IjkStatus.prepared; + } catch (e) { + _ijkStatus = IjkStatus.setDatasourceFail; + } } /// set asset DataSource