我写了一个Dart从服务器检索 .mp3 文件并播放它们的 Web 应用程序;我正在尝试使用 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";
}
}
我将如何重写dart:web_audio
Flutter 应用程序的部分代码? Flutter可以访问MediaPlayer吗?如果是这样,我将如何在其中引用它pubspec.yaml
?
正如 raju-bitter 上面指出的,Flutter 曾经在其核心引擎中提供一些内置的音频包装器,但后来这些已被删除:https://github.com/flutter/flutter/issues/1364 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://github.com/flutter/flutter/tree/master/examples/hello_services)。该模型记录在https://flutter.io/platform-services https://flutter.io/platform-services。这还远没有我们希望的那么容易。许多改进即将推出。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)