0

How to adjust the audio volume using the audio_service package? I don’t see any methods in the AudioHandler class that can do this.

For example, how can I adjust the volume with simple buttons on the Flutter UI, and/or change the volume gradually over say 5 seconds to fade in or out?

These operations are easy with just_audio by using the setVolume function, but I’m not sure how to integrate it with audio_service.

Here is my code for completeness.

class MyAudioHandler extends BaseAudioHandler {
  final _player = AudioPlayer();

  MyAudioHandler() {
    _player.playbackEventStream.map(updatePlaybackState).pipe(playbackState);
  }

  @override
  Future<void> play() async {
    await _player.play();
    return super.play();
  }

  @override
  Future<void> playMediaItem(MediaItem item) async {

    mediaItem.add(item);
    _player.setAudioSource(AudioSource.asset(item.id));
    await _player.play();
    return super.playMediaItem(item);
  }

  @override
  Future<void> stop() async {
    await _player.stop();
  }

  PlaybackState updatePlaybackState(PlaybackEvent event) {
    return PlaybackState(
      processingState: const {
        ProcessingState.idle: AudioProcessingState.idle,
        ProcessingState.loading: AudioProcessingState.loading,
        ProcessingState.buffering: AudioProcessingState.buffering,
        ProcessingState.ready: AudioProcessingState.ready,
        ProcessingState.completed: AudioProcessingState.completed,
      }[_player.processingState]!,
      playing: _player.playing,
      updatePosition: _player.position,
      repeatMode: AudioServiceRepeatMode.all
    );
  }
}


    return Scaffold(
      body: StreamBuilder<PlaybackState>(
          stream: _audioHandler.playbackState,
          builder: (context, snapshot) {
            bool isPlaying = snapshot.data?.playing ?? false;

            return Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                if (isPlaying)
                  ElevatedButton(
                    onPressed: () {
                      _audioHandler.stop();
                    },
                    child: Text('Stop'),
                  )
                else ...[
                  ElevatedButton(
                    onPressed: () async {
                      const item = MediaItem(
                        title: 'Sea',
                        id: 'assets/audio/seaside.mp3',
                      );

                      _audioHandler.playMediaItem(item);
                    },
                    child: Text('Play Seaside'),
                  ),
                ],
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    //// HOW TO ADJUST VOLUME FROM BUTTONS???

                    IconButton(onPressed: () {}, icon: Icon(Icons.add)),
                    IconButton(onPressed: () {}, icon: Icon(Icons.remove)),
                  ],
                ),
              ],
            );
          }),
    );
Kdon
  • 892
  • 6
  • 19

1 Answers1

0

AudioPlayer provides setVolume method which accept 0-1.

    player.setVolume(1.0);
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56