Entering background on iOS4 to play audio

后端 未结 2 2017
长情又很酷
长情又很酷 2020-11-29 01:53

The documentation is rather poorly written when talking about playing audio in the background. It gives the impression that all you have to do to continue playing the audio

相关标签:
2条回答
  • 2020-11-29 02:20

    The one part missing from the documentation is you need to set your audio session.

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    

    Add that and you'll be good to go.

    0 讨论(0)
  • 2020-11-29 02:27

    You also need to make calls to beginBackgroundTaskWithExpirationHandler: before you start a song (do it all the time whether in foreground or not) and endBackgroundTask when it ends.

    This tells iOS that you want to be able to continue processing even if your app gets put in the background. It does not create a new task or thread, it's just a flag to iOS that you want to keep running. The audio will play to completion without this, but when it completes if you want to start another track you must be able to run in the background.

    You can use it like this:

    // ivar, initialized to UIBackgroundTaskInvalid in awakeFromNib
    UIBackgroundTaskIdentifier bgTaskId; 
    

    When starting an audio player we save the bg task id:

    if ([audioPlayer play]) {
      bgTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
    }
    

    Now the audioPlayerDidFinishPlaying: callback will still occur because we have told iOS that we are doing a task that needs to continue even if we get put in the background. That way we get the callback and can start the next audio file.

    - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)success
    {
        UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
    
        if (self.haveMoreAudioToPlay) {
            newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
            [self playNextAudioFile];
        }
    
        if (bgTaskId != UIBackgroundTaskInvalid) {
            [[UIApplication sharedApplication] endBackgroundTask: bgTaskId];
        }
    
        bgTaskId = newTaskId;
    }      
    
    0 讨论(0)
提交回复
热议问题