IOS5, iOS6 Why does AVAudioPlayer stop playing when the screen locks?

I use AVAudioPlayer for several iOS applications, and they all play successfully after locking the screen until iOS6 (or iOS5) on my iPad2, now they stop. Here's how I set it up, which worked fine:

// 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]; 

Please note that I set the category to AVAudioSessionCategoryPlayback, which was the fix recommended earlier. This does not work since I upgraded to iOS6 - it also does not work on iOS5, as it turns out!

Update: I found an answer that works based on this post: AVAudioPlayer stops playing on the screen lock, even if the category is AVAudioSessionCategoryPlayback

Basically, I went to the "Information" page of the target and added a new key, "Necessary background modes", to this I added a value like the line type "Application plays audio." Now he makes his way through the track before turning off the screen completely.

+4
source share
1 answer

I found an answer that works based on this post:

AVAudioPlayer stops playing on the screen lock, even if the category AVAudioSessionCategoryPlayback

Basically, I went to the "Information" page of the target and added a new key, "Necessary background modes", to this I added a value like the line type "Application plays audio." Now he makes his way through the track before turning off the screen completely.

+1
source

All Articles