首页 > 技术文章 > 声音

pythonClub 2019-05-13 20:02 原文

https://stackoverflow.com/questions/43813386/how-to-play-a-custom-sound-in-flutter

 

Thanks for checking out Flutter!

Flutter SDK today (as of May 5, 2017) doesn't have built-in support to play and control arbitrary audio. However, we designed our plugin system to support it.

This plugin adds audio support to Flutter: https://pub.dartlang.org/packages/audioplayer

From the plugin's README:

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

推荐阅读