我有一個已在iTunes App Store上發佈的應用程序,並且已爲音頻啓用後臺模式。將應用程序升級到xcode後屏幕自動鎖定時,iOS應用程序音頻停止播放
更新到XCode 8後,我發佈了我的應用程序的更新,之後我發現應用程序在屏幕鎖定時停止播放。否則,我沒有對背景播放進行任何更改。不知道,如果行爲或編碼要求的iOS 9+
改變這裏是我的代碼做什麼:
App plist file:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>remote-notification</string>
</array>
AudioController.m
-(void)setBackgroundPlay:(bool)backgroundPlay
{
NSLog(@"setBackgroundPlay %d", backgroundPlay);
AVAudioSession *mySession = [AVAudioSession sharedInstance];
NSError *audioSessionError = nil;
if (backgroundPlay) {
// Assign the Playback category to the audio session.
[mySession setCategory: AVAudioSessionCategoryPlayback
error: &audioSessionError];
OSStatus propertySetError = 0;
UInt32 allowMixing = true;
propertySetError = AudioSessionSetProperty (
kAudioSessionProperty_OverrideCategoryMixWithOthers, // 1
sizeof (allowMixing), // 2
&allowMixing // 3
);
if (propertySetError != 0) {
NSLog (@"Error setting audio property MixWithOthers");
}
} else {
// Assign the Playback category to the audio session.
[mySession setCategory: AVAudioSessionCategoryPlayback
error: &audioSessionError];
}
if (audioSessionError != nil) {
NSLog (@"Error setting audio session category.");
}
}
音頻並繼續播放時,我儘量減少應用程序,並繼續打,直到屏幕自動鎖。每當屏幕開啓時(如接收到通知時),音頻恢復,然後在屏幕變黑時關閉。
如前所述,這玩意用來工作,似乎更新後,已經改變了行爲的Xcode 8/9的iOS
我試着搜索論壇和其他地方的人遇到類似的問題,但避風港無法找到任何東西。
任何建議,或一雙新鮮的眼睛看着這個將不勝感激!
感謝, 斯里達爾
您是否在項目設置的功能部分啓用了背景音頻? – raidfive
是的,我喜歡!我設法找到了問題和解決方案,我將在下面添加。 –