skip to Main Content

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
assetsaudiohe_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('assetsaudiohe_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

2

Answers


  1. 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….

    Login or Signup to reply.
  2. Try this

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

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search