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).
Steps:
Dependencies:
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())); }