Alan Moore
Alan Moore

Reputation: 6575

iOS5, iOS6 why does AVAudioPlayer playback stop on screen lock?

I've been using AVAudioPlayer for several iOS apps and they all successfully playback after the screen locks until iOS6(or iOS5) on my iPad2, now they stop. Here is how I set it up, which used to work 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];

Note that I set the category to AVAudioSessionCategoryPlayback which was the fix that was recommended previously. This isn't working since I've upgraded to iOS6 -- also not working on iOS5 as it turns out!

Update: I found an answer that appears to be working based on this post: AVAudioPlayer stops playing on screen lock even though the category is AVAudioSessionCategoryPlayback

Basically I went into the Info page of the target and added a new key "Required background modes", to this I added a string type value "App plays audio". Now it makes it all the way through the track before turning the screen off completely.

Upvotes: 4

Views: 1066

Answers (1)

Alan Moore
Alan Moore

Reputation: 6575

I found an answer that appears to be working based on this post:

AVAudioPlayer stops playing on screen lock even though the category is AVAudioSessionCategoryPlayback

Basically I went into the Info page of the target and added a new key "Required background modes", to this I added a string type value "App plays audio". Now it makes it all the way through the track before turning the screen off completely.

Upvotes: 1

Related Questions