How to play a custom sound in Flutter?

后端 未结 4 1230
感动是毒
感动是毒 2020-12-24 12:29

I was able to play a simple sound this line of code:

SystemSound.play(SystemSoundType.click);

How can I play a customized sound?

L

4条回答
  •  情书的邮戳
    2020-12-24 13:15

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

提交回复
热议问题