在你的 application:didFinishLaunchingWithOptions: 方法中,此方法在AppDelegate.m 中,加入如下代碼:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
據官方文檔, 這個 AVAudioSessionCategoryAmbient 作用是
當其他應用程序占用媒體播放器時,你的程序不能影響到后臺的媒體音樂播放
如果你想檢查你是否設置聲音的優先級成功了,可以用以下的代碼:
NSError *error; BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error]; if (!success) { //Handle error NSLog(@"%@", [error localizedDescription]); } else { // Yay! It worked! }
簡單說明下,可以做參考,就是當你的應用程序中有相機要拍照時,不會影響到后臺音樂程序的播放