Shalugin
Shalugin

Reputation: 1204

Flutter Audiocache - Change volume while playing

I was wondering if it's possible to change volume of Audiocache player in flutter on the go - without restarting current song.

In documentation volume is mentioned twice. You can set it when you call play or loop. None of that will work for me.

Also maybe I took the wrong path and there is a less painful way to play music with Flutter?

Any suggestions are highly appreciated. Thanks!

Upvotes: 1

Views: 4565

Answers (2)

Hamzq Am
Hamzq Am

Reputation: 21

try This inside initState..I know it deosn't make sense but it worked for me lol .

audioPlayer.onPlayerStateChanged.listen((event) {
  setState(() {
    isPlaying = event == PlayerState.PLAYING;
    audioPlayer.setVolume(1.0);
  });
});

Upvotes: 0

Shalugin
Shalugin

Reputation: 1204

I got it.

To be able to change volume we need to initiate instance of AudioPlayer that serves as kind of controller for AudioCache.

  static AudioPlayer advancedPlayer = AudioPlayer();
  static AudioCache player = AudioCache(fixedPlayer: advancedPlayer);

Then we can set volume by simply doing:

  void changeVolume(double value) {
    advancedPlayer.setVolume(value);
  }

Upvotes: 4

Related Questions