【问题标题】:Kotlin in Flutter application causing CrashFlutter 应用程序中的 Kotlin 导致崩溃
【发布时间】:2019-06-21 02:37:16
【问题描述】:

自上次 Flutter 更新以来,当我尝试使用媒体播放器插件时出现问题,女巫使用 Kotlin。

当您点击按钮并假设播放音频文件时,会导致应用程序崩溃。

fun play(url: String) {
    player.reset()
    channel.invokeMethod("onPosition", 0.0)
    player.setDataSource(url)
    player.prepareAsync()
    channel.invokeMethod("onIsLoading", null)
    task?.cancel()
    task = object : TimerTask() {
      override fun run() {
        if (player.isPlaying) {
          session.setPlaybackState(PlaybackStateCompat.Builder()
              .setState(PlaybackStateCompat.STATE_PLAYING, player.currentPosition.toLong(), 1f)
              .build())
            channel.invokeMethod("onPosition", player.currentPosition.toDouble() / player.duration.toDouble())
        }
      }
    }
    timer.schedule(task, 0, 100)
  }

有人建议我添加这个。

activity.runOnUiThread(java.lang.Runnable {
  channel.invokeMethod(...)
})

但如果我这样做,我会得到

Unresolved Reference: activity

这是错误信息。

java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: Timer-0
        at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:605)
        at io.flutter.embedding.engine.FlutterJNI.dispatchPlatformMessage(FlutterJNI.java:515)
        at io.flutter.embedding.engine.dart.DartMessenger.send(DartMessenger.java:76)
        at io.flutter.embedding.engine.dart.DartExecutor.send(DartExecutor.java:166)
        at io.flutter.view.FlutterNativeView.send(FlutterNativeView.java:155)
        at io.flutter.plugin.common.MethodChannel.invokeMethod(MethodChannel.java:98)
        at io.flutter.plugin.common.MethodChannel.invokeMethod(MethodChannel.java:84)
        at live.exit.musicplayer.MusicPlayer$play$1.run(MusicPlayer.kt:49)
        at java.util.TimerThread.mainLoop(Timer.java:562)
        at java.util.TimerThread.run(Timer.java:512)

我有点茫然,这应该是在播放音频。

我对 kotlin 一无所知。

【问题讨论】:

    标签: android kotlin flutter dart


    【解决方案1】:

    我认为您正在使用 ragotrebor 音乐播放器插件 (https://gitlab.com/ragotrebor)。查看他们的历史,大约一个月前进行了合并,应该可以解决这个问题。

    只需更新到最新版本的插件,您就可以开始使用了!

    背景

    这与 Flutter 的更改有关,以修复在主线程之外执行某些方法时导致 FlutterJNI(Java 原生接口)间歇性崩溃的错误。 Flutter 更改为在所有情况下都抛出异常,以避免这种间歇性崩溃并迫使开发人员在 UI 线程上正确运行方法。不幸的是,这导致许多现有插件崩溃,开发人员需要更新他们的源代码。

    参考链接

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效
    • @slfan 感谢您的反馈,我相应地更新了答案。希望这就是你的意思。
    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 2016-06-04
    • 2011-08-24
    • 2014-05-03
    相关资源
    最近更新 更多