Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复 prepared 后马上 play 无效的问题 #204

Merged
merged 3 commits into from
Mar 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions android/src/main/java/top/kikt/ijkplayer/Ijk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, Any>) {

Expand All @@ -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
Expand Down Expand Up @@ -214,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())
}
}

Expand All @@ -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)
Expand Down
16 changes: 10 additions & 6 deletions lib/src/controller/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down