Stop Background audio while starting

馋奶兔 提交于 2019-12-02 10:00:26

问题


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?


回答1:


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");
}


来源:https://stackoverflow.com/questions/20801775/stop-background-audio-while-starting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!