user2880019
user2880019

Reputation: 21

AVSpeechSynthesizer won't work when phone locked

I am trying to use AVSpeechSynthesizer when the phone is locked, but the audio stops when I lock the screen. I am using the simulator, not an actual device. I have seen a couple other questions similar to this on this site and I followed their recommendations, but it still does not work.

In the app delegate I set the audio session category to AVAudioSessionCategoryPlayback.

- (void)configureAudioSession{
    NSError *error = NULL;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];
    if(error) {
        NSLog(@"Error setting category of audio session: %@",error.description);
    }
    error = NULL;
    [[AVAudioSession sharedInstance] setActive:YES error: &error];
    if (error) {
        NSLog(@"Error activating audio session: %@",error.description);
    }
}

And I checked the 'Audio and Airplay' mode under Project Settings->Capabilities->Background Modes.

Can anyone tell me how to get this to work?

Upvotes: 0

Views: 1647

Answers (2)

Alan Scarpa
Alan Scarpa

Reputation: 3570

Here's how I got AVSpeechSynthesizer to continue speaking when phone goes idle, if phone gets locked, or if app goes to background. (iOS8)

Step 1) Open up info.plist and add the key "Required background modes". Within this array, add a String called "App plays audio or streams audio/video using AirPlay".

Step 2) Add the following to your app delegate didFinishLaunchingWithOptions:

NSError *error = NULL;
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback error:&error];
    if(error) {
        // Do some error handling
    }
    [session setActive:YES error:&error];
    if (error) {
        // Do some error handling
    }

Step 3) Run on your device and test it out!

Upvotes: 2

lehn0058
lehn0058

Reputation: 20257

I was able to follow this post to get it to work about a month ago:

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

Basically, you need to make entry in your app's .plist file. I'm not sure this will work on the simulator, so you will probably want to test it on an actual device.

Upvotes: 0

Related Questions