Playing MP3s on iPhone in Background - AudioQueue: Error

Hi I am developing an application that plays another mp3 buffer loop for 5 minutes. When the application runs in the foreground or in iphone standby mode, it plays normally, but when I try in the background, but it gives me this error:

Thu Feb 17 00:05:06 unknown mediaserverd [2303]: 00: 05: 06.158 AudioQueue: Error -12985 from AudioSessionSetClientPlayState (3582) Thu Feb 17 00:05:06 Unknown mediaserverd [2303]: 00: 05: 06.167 AudioQueue: Error -12985 of AudioSessionSetClientPlayState (3582)

Additional info: when I play buffered mp3 (first with root) in cicle, it plays well in the background. I am also trying to save buffered mp3 data to an mp3 file and then play, but this is the same problem. I searched for this error, but I did not find the relevant information.

Thanks in advance.

There (I think) the corresponding code:

-(void) prepareAudio {
    // Registers this class as the delegate of the audio session.
    [[AVAudioSession sharedInstance] setDelegate: self];

    // The AmbientSound category allows application audio to mix with Media Player
    // audio. The category also indicates that application audio should stop playing 
    // if the Ring/Siilent switch is set to "silent" or the screen locks.
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];

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

    /*UInt32 category = kAudioSessionCategory_AmbientSound;
    OSStatus result = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
    */
    // Activates the audio session.

    NSError *activationError = nil;
    [[AVAudioSession sharedInstance] setActive: YES error: &activationError];
}


- (void)finishedReceivingData:(NSData *)data {  
    if(data == nil) {
        NSLog(@"cdata nil");
        return;
    }
    if (player != nil) {
        [player release];
        player = nil;
    }

    player = [[AVAudioPlayer alloc] initWithData:data error: nil];
    [player setVolume:0.1];     
    [player play];
}
+3
source share

Source: https://habr.com/ru/post/1792683/


All Articles