我一直在爲幾個iOS應用程序使用AVAudioPlayer,它們都在屏幕鎖定之前成功回放,直到我的iPad2上的iOS6(或iOS5),現在它們停止。這是我如何設置它,它用來做工精細:iOS5,iOS6爲什麼AVAudioPlayer播放在屏幕鎖定時停止?
// Registers this class as the delegate of the audio session.
[[AVAudioSession sharedInstance] setDelegate: self];
// Use this code instead to allow the app sound to continue to play when the screen is locked.
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 0;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers,
sizeof (doSetProperty),
&doSetProperty
);
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
// Registers the audio route change listener callback function
AudioSessionAddPropertyListener (
kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback,
(__bridge void *)self
);
// Activates the audio session.
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];
// Instantiates the AVAudioPlayer object, initializing it with the sound
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: medURL error: nil];
self.appSoundPlayer = newPlayer;
// "Preparing to play" attaches to the audio hardware and ensures that playback
// starts quickly when the user taps Play
[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume: 0.5];
[appSoundPlayer setDelegate: self];
注意,我設置的類別AVAudioSessionCategoryPlayback這是以前推薦的修補程序。這不起作用,因爲我已升級到iOS6 - 也不適用於iOS5,因爲它變成了!
更新:我發現,似乎是基於對這個職位的工作答案:AVAudioPlayer stops playing on screen lock even though the category is AVAudioSessionCategoryPlayback
基本上我走進了目標的信息頁,並增加了新的密鑰「所需的背景模式」,這個我加字符串類型值「應用程序播放音頻」。現在,它可以在完全關閉屏幕之前完成整個軌道。