Flutter-尝试播放远程音频时应用崩溃

时间:2019-06-08 02:25:39

标签: android flutter dart

我有一个应用程序可以播放存储在Firestore上的音频文件。它曾经可以正常工作,然后突然间,仅在Android上可以正常工作,当您按下播放键时该应用就会崩溃。

我不确定以前的工作情况。

bool _isPlaying = false;
  bool _isPaused = false;
  MusicPlayer musicPlayer;
  double _duration = 1.0;
  double _endoftrack = 1.0;
  double _position = 0.0;
  double _playPosition = 0.0;
  double _percentBack = 0.0;
  double _percentForward = 0.0;


  @override
  void initState() {
    super.initState();
    initPlatformState();
    _duration = 1.0;
    _playPosition = 0.0;
  }

  // Initializing the Music Player and adding a single [PlaylistItem]
  Future<void> initPlatformState() async {
    musicPlayer = MusicPlayer();
  }

  play() async {
    await musicPlayer.play(MusicItem(
      trackName: widget.snapshot.data["sermonTitle"],
      albumName: widget.snapshot.data["series"],
      artistName: widget.snapshot.data["speaker"],
      url: widget.snapshot.data["audio"],
      coverUrl: widget.snapshot.data["image"],
      duration: Duration(seconds: _duration.toInt()),
    ));

    musicPlayer.onDuration = (d) => setState((){
      _duration = d.inSeconds.toDouble();
      debugPrint("---------------" + _duration.toString());
    });

    musicPlayer.onPosition = (p) => setState((){
      _position = p;
      _playPosition = _position * _duration;
      debugPrint("Play Position =" + _playPosition.toInt().toString());
      debugPrint("Duration =" + _duration.toString());
      debugPrint("Position =" + _position.toString());
      if(_position >= 1.0){
        debugPrint("Calling Stop!");
        complete();
      }
    });



    setState(() {
      _isPlaying = true;
    });
  }

  pause() async {
    await musicPlayer.pause();
    setState(() {
      _isPaused = true;
    _isPlaying = false;
    });
  }

  resume() async {
    await musicPlayer.resume();
    setState(() {
      _isPaused = false;
      _isPlaying = true;
    });
  }

  rewind() async {
    _percentBack = _position - (15/_duration);
    await musicPlayer.seek(_percentBack);
  }

  fastForward() async {
    _percentForward = _position + (15/_duration);
    if(_percentForward <= 1.0) {
      await musicPlayer.seek(_percentForward);
    }else {
      complete();
    }
  }

  complete() async{
    _position = 0;
    musicPlayer.stop();
    setState(() {
      _isPlaying = false;
    });
  }

以下是我从logcat得到的错误消息

2019-06-07 22:16:39.629 10012-10048/org.heartchurch.heartland E/AndroidRuntime: FATAL EXCEPTION: Timer-0
    Process: org.heartchurch.heartland, PID: 10012
    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)

我不确定发生了什么变化。再次在iOS上正常运行。

0 个答案:

没有答案