Search code examples
flutterdartjust-audio

Justaudio Flutter cant run


onTap: () {
final player = AudioPlayer();
player.play(
player.setAudioSource('assets/sound.wav'),
);

There was an error in my just auido flutter so I can't run the audio. help me please. I create my code in vscode and i had add asset in public yaml and import the package.


Solution

  • setAudioSource expects to be passed an AudioSource. You are passing it a string.

    Instead you can use setAsset with the string parameter, which will take care of creating the AudioSource and calling setAudioSource for you.

    onTap: () {
        final player = AudioPlayer();
        player.setAsset('assets/sound.wav').then( () => {
            player.play();
        } ),
    },
    

    Please note that creating a new AudioPlayer instance on every tap is not a good idea, instead you should create the player just once and re-use it.