2

So my app is running in the background because of CoreLocation that has requested AlwaysAuthorization. This prevents app to reach suspended state. It stays in background state and is receiving location events. After some location events I would like to activate AVAudioSession and play some sounds. How to activate session properly (in this background app) when I'm using other app in foreground right now and this app is playing audio track for example. Suppose I'm watching some video in Youtube app and my background app needs to play audio right now. I've problems in activating audio session in this case.

I can't use AudioToolbox playing engine in this case because those sounds needs to be played even when Ring/Silent switch is on.

I know I need to use AVAudioSessionCategoryPlayback category. I also don't want other apps to be played in the same time so I'm not using MixWithOthers or DuckOthers category options.

I have enabled UIBackgroundModes for audio playback in background.

How to activate such session when app is in background and other app currently is playing audio without any mixing options enabled. How to interrupt audio session in current foreground app so my background app can activate it for playback time. After playing is over my plan is to deactivate audio session in background and notify other apps that they can resume theirs playback.

Marcin Kapusta
  • 5,076
  • 3
  • 38
  • 55
  • Late comment to the game. This is theoretical on my end, but if you are able to run foreground code as reaction to some location update delegate then playing the sound through wkwebview through html audio element could be your magic just-in-time `AVAudioSessionCategoryPlayback` trick and would kick back to `AVAudioSessionCategoryAmbient` once the sound is done. See my answer here: https://stackoverflow.com/a/56460400/5329717 – Kamil.S Mar 18 '23 at 11:13

2 Answers2

0

Add these below 3 lines in the didFinishLaunchingWithOptions method of AppDelegate.m class and test the app again. Let me know if there is any problem regarding the same.

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[UIApplication sharedApplication].idleTimerDisabled = YES;
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • Activating session just after app is launched is not the best practices. It also doesn't work. I tried. When I launch app this way and switch to Music App, play some music. My app gets interruption of audio session (Yes I'm handling interruptions correctly in my app) and in this state (with Music App in Foreground and my App in the background) I'm waiting for my app to get the location trigger and start playing some audio. The app in the background cannot simply start audio session with not mixable category. – Marcin Kapusta Jan 26 '17 at 19:01
0

Thank you for enlightening me with your question. I want to play video continuously. When the App is playing the next video in the background, AVAudioSession cannot be activated as expected and there is no sound. So I'll start by using MixWithOthers

[[AVAudioSession sharedInstance] setActive:NO error:&activeError]]
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];

Activate it

[[AVAudioSession sharedInstance] setActive:YES error:&activeError]]

Remove category options

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:0 error:nil];

everything is back on track.

Liam
  • 1,712
  • 1
  • 17
  • 30
zm l
  • 1
  • 2