微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android-如何在Flutter应用中播放.mp3文件?

我编写了一个Dart Web应用程序,该应用程序从服务器检索.mp3文件并进行播放;我正在尝试使用Flutter编写移动版本.我知道dart:web_audio是Web应用程序的主要选项,但是Flutter在我的SDK中找不到它.我知道它在那里,因为我可以将以下内容编译为Javascript

    import 'dart:html';
    import 'dart:convert';
    import 'dart:web_audio';
    AudioContext audioContext;

    main() async {
      audioContext = new AudioContext();
      var ul = (querySelector('#songs') as UListElement);
      var signal = await HttpRequest.getString('http://10.0.0.6:8000/api/filelist');
     //  Map json = JSON.decode(signal);
     //  for (Map file in json['songs']) {
       print("signal: $signal");
       Map json = JSON.decode(signal);
       for (Map file in json['songs']) {
         var li = new LIElement()
           ..appendText(file['title']);
         var button = new ButtonElement();
         button.setAttribute("id", "#${file['file']}");
         button.appendText("Play");

         li.append(button);
         new Song(button, file['file']);
         ul.append(li);

      }

    }


    class Song {
      ButtonElement button;
      bool _playing = false;
      // AudioContext _audioContext;
      AudioBufferSourceNode _source;
      String title;

      Song(this.button, this.title) {

        button..onClick.listen((e) => _toggle());
      }

      _toggle() {
        _playing = !_playing;
        _playing ? _start() : _stop();
      }

      _start() {
        return HttpRequest
             .request("http://10.0.0.6:8000/music/$title", responseType: "arraybuffer")
             .then((HttpRequest httpRequest) {
                return audioContext
                  .decodeAudioData(httpRequest.response)
             .then((AudioBuffer buffer) {
                  _source = audioContext.createBufferSource();
                  _source.buffer = buffer;
                  _source.connectNode(audioContext.destination);
                  _source.start(0);
                  button.text = "Stop";
                  _source.onEnded.listen((e){
                     _playing = false;
                     button.text = "Play";
              });
           });
        });
      }

      _stop() {
         _source.stop(0);
         button.text = "Play";
      }
    } 

如何为Flutter应用程序重写代码中的dart:web_audio部分? Flutter可以访问MediaPlayer吗?如果是这样,我将如何在pubspec.yaml中引用它?

解决方法:

正如上面提到的raju-bitter一样,Flutter曾经在其核心引擎中提供了一些内置的音频包装器,但是后来被删除了:https://github.com/flutter/flutter/issues/1364.

使用Flutter的应用程序只是iOS或Android应用程序,因此可以使用hello_services模型(https://github.com/flutter/flutter/tree/master/examples/hello_services)中的某些Java或Obj-C代码通过Flutter完成底层iOS / Android可以执行的任何操作.该模型记录在https://flutter.io/platform-services.它还没有我们想要的那么容易.许多改进即将推出.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐