一尘不染

如何在Flutter中播放自定义声音?

flutter

我能够在这行代码中播放简单的声音:

SystemSound.play(SystemSoundType.click);

如何播放自定义声音?

假设有一个简短的mp3


阅读 791

收藏
2020-08-13

共1个答案

一尘不染

感谢您检查Flutter!

今天(截至2017年5月5日)的Flutter SDK不具有播放和控制任意音频的 内置 支持。但是,我们设计了插件系统来支持它。

该插件为Flutter添加了音频支持:https//pub.dartlang.org/packages/audioplayer

从插件的自述文件中:

Future play() async {
  final result = await audioPlayer.play(kUrl);
  if (result == 1) setState(() => playerState = PlayerState.playing);
}

// add a isLocal parameter to play a local file
Future playLocal() async {
  final result = await audioPlayer.play(kUrl);
  if (result == 1) setState(() => playerState = PlayerState.playing);
}


Future pause() async {
  final result = await audioPlayer.pause();
  if (result == 1) setState(() => playerState = PlayerState.paused);
}

Future stop() async {
  final result = await audioPlayer.stop();
  if (result == 1) {
    setState(() {
      playerState = PlayerState.stopped;
      position = new Duration();
    });
  }
}
2020-08-13