How to play a custom sound in Flutter?

后端 未结 4 1202
感动是毒
感动是毒 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:08

    Simple solution for playing a file already defined in assets is using AudioCache. Library: https://pub.dartlang.org/packages/audioplayers. More about AudioCache After adding library to pubspec.yaml, import required class:

    import 'package:audioplayers/audio_cache.dart';
    

    add an asset in the same file and place the file with sound to assets folder (if you don't have this folder, create it)

    assets:
    - assets/sound_alarm.mp3
    

    then add this code:

    static AudioCache player = new AudioCache();
    const alarmAudioPath = "sound_alarm.mp3";
    player.play(alarmAudioPath);
    

    An example here

    0 讨论(0)
  • 2020-12-24 13:08

    The audioplayers works (from https://medium.com/@bennett4/adding-custom-sound-effects-to-a-flutter-mobile-app-41594f1f3305):

    (1) Add the library to your pubspec.yaml: audioplayers: ^0.15.1

    (2) In pubspec.yaml under flutter add the reference to your assets file:

    flutter
        assets:
           - assets/yes.mp3
    

    MAKE SURE it is under assets folder. It does not work when it is in a sub folder. For example, something like: - assets/sounds/yes.mp3 will not work. Just put your audio file in the assets folder not in its sub folder

    (3) import the library in your app as: import package:audioplayers/audioplayers.dart;

    (4) then define this function:

    Future<AudioPlayer> playLocalAsset() async {
        AudioCache cache = new AudioCache();
       //At the next line, DO NOT pass the entire reference such as assets/yes.mp3. This will not work.
       //Just pass the file name only.
        return await cache.play("yes.mp3"); 
    }
    

    (5) call the function whenever you need to play a sound: await playLocalAsset();

    0 讨论(0)
  • 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();
        });
      }
    }
    
    0 讨论(0)
  • 2020-12-24 13:29

    [Answer updated: this approach doesn't work, see comments] You can use the video_player plugin maintained by the Flutter team. It can reproduce many kinds of media across platforms, including sound files. More specifically, you may want to use the the VideoPlayerController class.

    eg.

       _controller = VideoPlayerController.network('https://www.example.com/soundsFile.wav');
       _controller.play();
    

    0 讨论(0)
提交回复
热议问题