1

I need to play audio without interrupting an existing playlist. My goal is to play an audio over the current audio service that is going on the phone, ideally I would like to duck the audio and play my stream over it. Pretty much like GPS services do while driving (Waze, Google maps).

  • I think this could be 2 separate questions. 1) How to play background audio, 2) How to duck the audio of other apps. Have you tried searching for existing answers to these individual questions? – Ryan Heise Aug 28 '21 at 13:33
  • Hi @RyanHeise! Thanks for the comment. I'm already able to play background audio, there are multiple ways. I'm using just_audio: latest & just_audio_background: ^0.0.1-beta.0+1. I'm asking this question because I have tried multiple configurations on the audio session but none have worked. All of them interrupt the audio that is playing at the moment. If you have any existing answer I would really appreciate it. – Paulo Briceño Gonzalez Aug 29 '21 at 00:54
  • @RyanHeise just realized you were the one who wrote those packages. Thanks you are amazing! – Paulo Briceño Gonzalez Aug 29 '21 at 01:09
  • No worries - Ducking works completely independently of background audio, so you don't need to mention background audio, and you can narrow your question to just talk about ducking so that it will be easier for someone to answer (although note that there are already many related answers if you search for ducking on Android or iOS, and audio_session basically exposes the raw Android and iOS APIs directly so any answer that's specific to Android and iOS on StackOverflow will also apply to audio_session.) – Ryan Heise Aug 29 '21 at 12:17

1 Answers1

2

Steps:

  1. Install dependencies
  2. Create initAudioService() function
  3. Create an AudioSession instance
  4. Configure your AudioSession instance as in the example
  5. Initialize your AudioService as in the example
  6. Call initAudioService() from main before runApp

Dependencies:

  • audio_service: ^0.18.0;
  • audio_session: ^0.1.6+1

Example:


    import 'package:audio_service/audio_service.dart';
    import 'package:audio_session/audio_session.dart';
    import 'audio_player_service.dart';
    
    Future initAudioService() async {
      //audio_session INSTANCE
      final session = await AudioSession.instance;
      //audio_session DUCK OTHERS CONFIGURATION
      await session.configure(const AudioSessionConfiguration(
        avAudioSessionCategory: AVAudioSessionCategory.playback,
        avAudioSessionCategoryOptions: AVAudioSessionCategoryOptions.duckOthers,
        avAudioSessionMode: AVAudioSessionMode.defaultMode,
        avAudioSessionRouteSharingPolicy:
            AVAudioSessionRouteSharingPolicy.defaultPolicy,
        avAudioSessionSetActiveOptions: AVAudioSessionSetActiveOptions.none,
        androidAudioAttributes: AndroidAudioAttributes(
          contentType: AndroidAudioContentType.music,
          flags: AndroidAudioFlags.none,
          usage: AndroidAudioUsage.media,
        ),
        androidAudioFocusGainType: AndroidAudioFocusGainType.gainTransientMayDuck,
        androidWillPauseWhenDucked: true,
      ));
    //INITIALIZE audio_service
      return await AudioService.init(
        builder: () => AudioPlayerService(),
        config: const AudioServiceConfig(
          androidNotificationChannelId: 'com.YOUR_COMPANY.YOUR_APP_NAME.audio',
          androidNotificationChannelName: 'Audio Service Demo',
          androidNotificationOngoing: true,
          androidStopForegroundOnPause: true,
        ),
      );
    }
    
    //MAIN
    void main() async {
      await initAudioService();
      runApp(const MaterialApp(home: MyApp()));
    }