问题
I did some preliminary test and have a good idea the answer is no. But just need to confirm: Can a background and foreground app share audio playback device? (The background app will be mine. The foreground app will be from third party)
回答1:
That is possible and here is how:
Make sure that app continues playing audio when left in background by doing this:
a) add the following to your Info plist file:
"Required background modes" "Item 0" -> "App plays audio"
b) Call setCategory:error: for AVAudioSession of your app:
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
Allow your audio to be mixed with audio from other apps by calling AudioSessionSetProperty():
OSStatus propertySetError = 0;
UInt32 allowMixing = true;
propertySetError = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (allowMixing), &allowMixing );
You will need to link against AVFoundation and AudioToolbox frameworks for this code.
回答2:
Technically this is possible. While a background app is playing music, the foreground app is still allowed to play sound effects (like the "Music" app is playing music while you hear the sound effects of the game you're playing foreground). So you could declare the music a foreground app plays as a sound effect while another app plays music in background. But if you can't control the foreground app, to get them play their music as a sound effect, than there is no way.
I also doubt that an app that declared continuos music a sound effect will make it through Apple review process.
来源:https://stackoverflow.com/questions/19069277/background-and-foreground-apps-using-audio