0

I'm using an AudioPlayer package to develop the audio/media player. To load the music from the asset folder, I'm using an AudioCache class, and to utilize other methods (pause, stop, and duration), I'm using AudioPlayer.

When I click on the button to play the music, it works perfectly but when I click again on that button to pause the music, then it didn't work at all (music keeps playing) and neither icons are changing on the click

What am I doing wrong?

Here is the code

class Music extends StatefulWidget {

  @override
  _MusicState createState() => _MusicState();
}

class _MusicState extends State<Music> {

  Duration duration = new Duration();
  Duration position = new Duration();
  AudioCache player = AudioCache();  // AudioCache 
  AudioPlayer audioPlayer = AudioPlayer(); // AudioPlayer
  bool playing = false;


  void getAudio() async{

    if(playing){

      var result = await audioPlayer.pause();

      if(result == 1) {
        setState(() {
          playing = false;
        });
      }

    }else{

      var result = await player.play('audio/music.mp3'); // to play the music from asset folder or local file

      if(result == 1) {
        setState(() {
          playing = true;
        });
      }
    }

    audioPlayer.onDurationChanged.listen((Duration dd){
      setState(() {
        duration = dd;
      });
    });

    audioPlayer.onAudioPositionChanged.listen((Duration dd){
      setState(() {
        position = dd;
      });
    });

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('App Name', style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontFamily: 'oswald'),),
        centerTitle: true,
        flexibleSpace: Image(
          image: AssetImage('images/bg.jpg'),
          fit: BoxFit.cover,
        ),
      ),
      backgroundColor: Colors.grey[200],
      body: SafeArea(
          child: Container(
            padding: EdgeInsets.all(25.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Slider(
                  value: position.inSeconds.toDouble(),
                  min: 0,
                  max: duration.inSeconds.toDouble(),
                  activeColor: Colors.deepOrange,
                  onChanged: (double value){
                    setState(() {
                      audioPlayer.seek(new Duration(seconds: value.toInt()));
                    });
                  },
                ),
                SizedBox(height: 10.0,),
                Ink(
                  decoration: const ShapeDecoration(
                    color: Colors.deepOrange,
                    shape: CircleBorder(),
                  ),
                  child: IconButton(
                    icon: Icon(playing == false ? Icons.play_arrow : Icons.pause_circle),
                    iconSize: 50.0,
                    color: Colors.white,
                    onPressed: () {
                        getAudio(); // call function
                    },
                  ),
                ),
              ],
            ),
          ),
      ),
    );
  }
}

shakky
  • 434
  • 5
  • 20

1 Answers1

0

You need set your result to audioplayer attribute:

if (result.state == PlayerState.PLAYING) {
    setState(() {
      playing = true;
    });
  }
  audioPlayer = result;