I could make the application, to play the audio when it moves to the background, by adding the setting in info.plist file.

But, when some other iOS application plays background audio, and when the current application comes to foreground, how to stop the background audio invoked by other application?

有帮助吗?

解决方案

Have a look at AVAudioSession class and AVAudioSession programming guide by Apple. This will hopefully steer you in the right direction.

To stop background audio when your application starts set your audio session to a non-mixable category and mode then set active.

If you don't configure an audio session your application will get the default session which in your case is not what you're after.

Try this in you applicationDelegates method - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

// set audio session category AVAudioSessionCategoryPlayAndRecord with no options
success = [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error:nil];
if (!success) {
    NSLog(@"setCategoryError");
}

// set audio session mode to default
success = [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:nil];
if (!success) {
    NSLog(@"setModeError");
}

// activate audio session
success = [[AVAudioSession sharedInstance] setActive:YES error:nil];
if (!success) {
    NSLog(@"activationError");
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top