Skip to content

[Question]: How to listen to audio guidance events #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mosesgameli opened this issue Mar 16, 2025 · 3 comments
Open

[Question]: How to listen to audio guidance events #309

mosesgameli opened this issue Mar 16, 2025 · 3 comments
Assignees
Labels
triage me I really want to be triaged. type: question Request for information or clarification. Not an issue.

Comments

@mosesgameli
Copy link

mosesgameli commented Mar 16, 2025

I'm building an app where users can play our radio during a navigation session. I want to listen for audio guidance events in order to duck the radio volume.

@mosesgameli mosesgameli added triage me I really want to be triaged. type: question Request for information or clarification. Not an issue. labels Mar 16, 2025
@mosesgameli mosesgameli changed the title [Support Request]: [Question]: How to listen to audio guidance events Mar 16, 2025
@altaf2892
Copy link

@jokerttu @mosesgameli can we get response on this .

@mosesgameli
Copy link
Author

Hi @altaf2892

For the time being, I decided to manually handle audio interruptions using audio_session package

@altaf2892
Copy link

altaf2892 commented Mar 28, 2025

Hi @altaf2892

For the time being, I decided to manually handle audio interruptions using audio_session package

Can u please share part of code .. might help me.

_i tried to implement just roughly _
added below code. but i m not getting any logs now i dont know why it it happening

AudioSession.instance.then((audioSession) async {
      // This line configures the app's audio session, indicating to the OS the
      // type of audio we intend to play. Using the "speech" recipe rather than
      // "music" since we are playing a podcast.
      await audioSession.configure(AudioSessionConfiguration.speech());
      // Listen to audio interruptions and pause or duck as appropriate.
      _handleInterruptions(audioSession);
      // Use another plugin to load audio to play.
      await _player.setUrl(
          "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3");
    });


  void _handleInterruptions(AudioSession audioSession) {
    // just_audio can handle interruptions for us, but we have disabled that in
    // order to demonstrate manual configuration.
    bool playInterrupted = false;
    audioSession.becomingNoisyEventStream.listen((_) {
      debugPrint('PAUSE');
      _player.pause();
    });
    _player.playingStream.listen((playing) {
      playInterrupted = false;
      if (playing) {
        audioSession.setActive(true);
      }
    });
    audioSession.interruptionEventStream.listen((event) {
      debugPrint('interruption begin: ${event.begin}');
      debugPrint('interruption type: ${event.type}');
      if (event.begin) {
        switch (event.type) {
          case AudioInterruptionType.duck:
            if (audioSession.androidAudioAttributes!.usage ==
                AndroidAudioUsage.game) {
              _player.setVolume(_player.volume / 2);
            }
            playInterrupted = false;
            break;
          case AudioInterruptionType.pause:
          case AudioInterruptionType.unknown:
            if (_player.playing) {
              _player.pause();
              playInterrupted = true;
            }
            break;
        }
      } else {
        switch (event.type) {
          case AudioInterruptionType.duck:
            _player.setVolume(min(1.0, _player.volume * 2));
            playInterrupted = false;
            break;
          case AudioInterruptionType.pause:
            if (playInterrupted) _player.play();
            playInterrupted = false;
            break;
          case AudioInterruptionType.unknown:
            playInterrupted = false;
            break;
        }
      }
    });
    audioSession.devicesChangedEventStream.listen((event) {
      debugPrint('Devices added: ${event.devicesAdded}');
      debugPrint('Devices removed: ${event.devicesRemoved}');
    });
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage me I really want to be triaged. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants