Skip to content Skip to sidebar Skip to footer

How To Play An Audio File In Flutter?

I'm new in flutter development and I got stuck in a program where I want to play an audio file on pressing a TextButton(), but on pressing that button no audio is played and got a

Solution 1:

Add this Dependency to your pubspec.yaml file assets_audio_player: ^3.0.3+3

create playSound function like

playSound() async {
      AssetsAudioPlayer audioPlayer = AssetsAudioPlayer();
      audioPlayer.open(Audio('assets/bells.mp3'));
}

inside widget create button

TextButton(
        onPressed: () => playSound(),
        child: Text('Play'),
      ),

If Some error occurred go to our project directory => android\app\build.gradle and change compileSdkVersion 28, minSdkVersion 21, targetSdkVersion 30 It solve your problem Your build.gradle file here

Post a Comment for "How To Play An Audio File In Flutter?"