Search code examples
flutteraudio-playerandroid-music-player

How to play multiple audio files simultaniously in flutter


I have been trying to run the multiple audio files at the same time each having a separate mute button. I have tried AudioPlayer Flutter plugin. multiple audio files player I have tried the following code but it makes the files run one by one, not simultaniously.
Please help!!

  Future play() async {
    await audioPlayer.play(url);
    await audioPlayer2.play(url2);
    setState(() {
      playerState = AudioPlayerState.PLAYING;
    });
  }


Solution

  • May be their are more appropriate solutions but this solved my problem at the moment, I have used the plugin audioplayers and created multiple instances for each audio file.
    Updated: For example for two audio files it's done like below:

    enum PlayerState { stopped, playing, paused } 
    enum PlayerState1 { stopped1, playing1, paused1 }
    
    class AudioScreen extends StatefulWidget {
     
      @override
      _AudioScreenState createState() =>_AudioScreenState();
    }
    
    class _AudioScreenState extends State<AudioScreen> {
      //for first audio
      AudioPlayer audioPlayer;
      PlayerState playerState = PlayerState.stopped;
      get isPlaying => playerState == PlayerState.playing;
      //for second audio 
      AudioPlayer audioPlayer1;
      PlayerState1 playerState1 = PlayerState1.stopped1;
      get isPlaying1 => playerState1 == PlayerState1.playing1;
    
      @override
      void initState() {
        super.initState();
        audioPlayer = new AudioPlayer();
        audioPlayer1 = new AudioPlayer();
      }
       Future play() async {
        await audioPlayer.play(url);
        setState(() {
          playerState = PlayerState.playing;
         });
        await audioPlayer1.play(url1);
        setState(() {
          playerState1 = PlayerState1.playing1;
         });
      }
       Future stop() async {
        await audioPlayer.stop();
          setState(() {
          playerState = PlayerState.stopped;
          });
        await audioPlayer1.stop();
          setState(() {
          playerState1 = PlayerState1.stopped1;
          });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body:Column(
           children:[
            IconButton(
              onPressed:() play(),
              iconSize: 70.0,
              icon:Icon(Icons.play_circle_outline, size: 55),
            ),
            IconButton(
              onPressed:  stop(),
              iconSize: 55.0,
              icon: Icon(Icons.stop),
            ),
           ]
          ),
         );
      }
    }

    This way you can multiple instances or just use a loop to create multiple instances for each audio.