Home > Enterprise >  how to use audioplayers package to play local audio flutter
how to use audioplayers package to play local audio flutter

Time:01-01

I want to use the audio players package to run audio files on my app,
NOTE:

  • I use local files in assets file like this
assets\audio\he_has_got_the_packa.mp3

this path file
I try to use AssetSource

// this is an object from AudioPlayer
AudioPlayer player = AudioPlayer();
...

TextButton(
  onPressed: () {
       player.setSource(AssetSource('assets\audio\he_has_got_the_packa.mp3'));
    },
       child: Text('to play click here'),
   ),

but isn't running because asynchronous suspension
I try searching but I don't get content to this thing

CodePudding user response:

you use below code and you issue is fix.

            TextButton(
              onPressed: () {
                player.setSource(AssetSource('sample-15s.mp3')).then((value) {
                  player.play(AssetSource('sample-15s.mp3'));
                });
              },
              child: Text('to play click here'),
            ),

also please verify this image also.

enter image description here

Happy Codeing....

CodePudding user response:

Try this

TextButton(
  onPressed: () async {
       await player.play(AssetSource('assets\audio\he_has_got_the_packa.mp3'));
    },
       child: Text('to play click here'),
   ),

Adding await keyboard and making onPressed() async is important.

  • Related