Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

ios - AVplayer resuming after incoming call

I am using AVPlayer for music playback. My problem is that after an incoming call, the player won't resume. How do I handle this when an incoming call comes?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Starting from iOS 6 you must handle AVAudioSessionInterruptionNotification and AVAudioSessionMediaServicesWereResetNotification, before this you had to use delegate methods.

First you should call the AVAudioSession singleton and configure it for your desired use.

For example:

AVAudioSession *aSession = [AVAudioSession sharedInstance];
[aSession setCategory:AVAudioSessionCategoryPlayback
          withOptions:AVAudioSessionCategoryOptionAllowBluetooth 
                error:&error];
[aSession setMode:AVAudioSessionModeDefault error:&error];
[aSession setActive: YES error: &error];

Then you should implement two methods, for the notifications which the AVAudioSession would call:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleAudioSessionInterruption:)
                                             name:AVAudioSessionInterruptionNotification
                                           object:aSession];

First one is for any interruption which would be called because of an incoming call, alarm clock, etc.

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleMediaServicesReset)
                                             name:AVAudioSessionMediaServicesWereResetNotification
                                           object:aSession];

The second one if the media server resets for any reason, you should handle this notification to reconfigure audio or do any housekeeping. By the way the notification dictionary won't contain any object.

Here is an example for handling the playback interruption:

- (void)handleAudioSessionInterruption:(NSNotification*)notification {

    NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
    NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey];

    switch (interruptionType.unsignedIntegerValue) {
        case AVAudioSessionInterruptionTypeBegan:{
            // ? Audio has stopped, already inactive
            // ? Change state of UI, etc., to reflect non-playing state
        } break;
        case AVAudioSessionInterruptionTypeEnded:{
            // ? Make session active
            // ? Update user interface
            // ? AVAudioSessionInterruptionOptionShouldResume option
            if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
                // Here you should continue playback.
                [player play];
            }
        } break;
        default:
            break;
    }
}

Notice that you should resume playback when the optional value is AVAudioSessionInterruptionOptionShouldResume

And for the other notification you should take care of the following:

- (void)handleMediaServicesReset {
// ? No userInfo dictionary for this notification
// ? Audio streaming objects are invalidated (zombies)
// ? Handle this notification by fully reconfiguring audio
}

Regards.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...