私はいくつかのiOSアプリにAVAudioPlayerを使用してきましたが、画面がロックされた後、iPad2のiOS6(またはiOS5)まですべて正常に再生され、停止します。これが私がそれを設定する方法です、それは以前はうまく機能していました:
// 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でも機能していません。
更新:この投稿に基づいて機能しているように見える回答を見つけました:カテゴリがAVAudioSessionCategoryPlaybackであっても、AVAudioPlayerは画面ロックで再生を停止します
基本的に、ターゲットの情報ページに移動し、新しいキー「必要なバックグラウンドモード」を追加しました。これに、文字列型の値「アプリがオーディオを再生します」を追加しました。これで、画面を完全にオフにする前に、トラック全体に到達します。