2

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;
    });
  }
kiran
  • 100
  • 2
  • 10

1 Answers1

1

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.

kiran
  • 100
  • 2
  • 10
  • Could you please share how you achieve this..? – Wikki Jan 18 '21 at 19:50
  • sorry for late response, have you done it? – kiran Jan 26 '21 at 14:16
  • @kiran Hi I would like to ask, are you able to play it in background ? I search everywhere about audioplayers and audio_service to run in the background, but still did'n get any resource to do that – wahyu Jan 09 '22 at 07:28