如何在Flutter应用中播放.mp3文件? [英] How to playback an .mp3 file in a Flutter app?
问题描述
我编写了一个 Dart 网络应用程序,该应用程序从服务器检索.mp3文件并进行播放;我正在尝试使用Flutter编写移动版本.我知道dart:web_audio
是Web应用程序的主要选项,但是Flutter在我的SDK中找不到它.我知道它在那里,因为我可以将以下内容编译为Javascript:
I have written a Dart web app that retrieves .mp3 files from a server and plays them back; I am trying to write a mobile version using Flutter. I know dart:web_audio
is the main option for a web app, but Flutter can't find it in my SDK. I know it's there because I can compile the following to 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
中引用它?
How would I rewrite the dart:web_audio
parts of my code for a Flutter app? Can Flutter access MediaPlayer? And if so, how would I refer to it in pubspec.yaml
?
推荐答案
正如上面提到的raju-bitter一样,Flutter过去在其核心引擎中提供了一些内置的音频包装器,但是后来被删除了: https://github.com/flutter/flutter/issues/1364 .
As raju-bitter noted above, Flutter used to provide some built-in audio wrappers in its core engine but those have since been removed: https://github.com/flutter/flutter/issues/1364.
使用Flutter的应用程序只是iOS或Android应用程序,因此可以使用hello_services模型中的某些Java或Obj-C代码通过Flutter完成底层iOS/Android可以做的任何事情( https://flutter.io/platform-services 中记录了该模型.这远不像我们希望的那样容易.许多改进即将推出.
Flutter-using Apps are just iOS or Android apps, and thus it is possible to do anything the underlying iOS/Android can do via Flutter using some Java or Obj-C code in the hello_services model (https://github.com/flutter/flutter/tree/master/examples/hello_services). This model is documented at https://flutter.io/platform-services. It's not nearly as easy as we'd like it to be yet. Many improvements to come soon.
这篇关于如何在Flutter应用中播放.mp3文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!