如何在Flutter应用程序中播放.mp3文件?

时间:2016-12-08 04:49:49

标签: android audio dart flutter

我编写了一个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中引用它?

2 个答案:

答案 0 :(得分:3)

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

使用Flutter的应用程序只是iOS或Android应用程序,因此可以使用hello_services模型中的某些Java或Obj-C代码通过Flutter执行底层iOS / Android可以执行的任何操作(https://github.com/flutter/flutter/tree/master/examples/hello_services) 。该模型记录在https://flutter.io/platform-services。它并不像我们希望的那样容易。很快就会有很多改进。

答案 1 :(得分:0)

我知道它晚了4年,但是我发现了audioplayers软件包,可以用作以下软件包

import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';

//Call this function from an event
void playRemoteFile() {
    AudioPlayer player = new AudioPlayer();
    player.play("https://luan.xyz/files/audio/ambient_c_motion.mp3");
}