5

我编写了一个Dart网络应用程序,它从服务器检索 .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

4

2 回答 2

4

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

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

于 2016-12-08T21:02:52.370 回答
1

我知道它晚了 4 年,但我找到了可以用作以下内容的音频播放器包

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");
}

于 2020-05-08T05:51:43.677 回答