iOS电话等中断事件的开始和结束通知

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAudioSessionInterruptNoti:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];

}

//音频播放中断的通知

-(void)handleAudioSessionInterruptNoti:(NSNotification*)noti

{

    NSDictionary *userInfo = noti.userInfo;

    AVAudioSessionInterruptionType type = [userInfo[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];

    switch (type)

    {

        case AVAudioSessionInterruptionTypeBegan:

        {

            //需要停止播放操作

        }

            break;

        case AVAudioSessionInterruptionTypeEnded:

        {

            AVAudioSessionInterruptionOptions options = [userInfo[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];

            if (options == AVAudioSessionInterruptionOptionShouldResume)

            {

                //需要执行播放操作

            }

        }

            break;

        default:

            break;

    }

}

 

 

posted @ 2018-02-28 10:06  雨筱逸悠  阅读(335)  评论(0编辑  收藏  举报