I am a beginner with flutter and can't get the audio to stop playing using the same button that initialises it. The audio is meant to start playing with the first press and toggles on/off with the same button after. The icon is also meant to toggle between volume_off and volume_up.
I previously tried using a boolean variable which would change value with every press that switches between the music mp3 file and a silent mp3 file along with the icons.
Container(
alignment: Alignment.topLeft,
child: IconButton(
icon: Icon(Icons.volume_up),
color: Colors.white,
splashColor: Colors.black,
iconSize: 50.0,
onPressed: () {
final player=AudioCache();
player.play('seamusic.mp3');
},
),
),
You can achieve the desired result with a boolean variable. Try the code below.
The above code restarts the music from the beginning every time. If you want to resume from where it left off, you can use the code snippet below.
You should visit here for more information.