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(handleAudioSessionRouteChangeNoti:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];
}
//音频播放中断的通知
-(void)handleAudioSessionRouteChangeNoti:(NSNotification*)noti
{
    NSDictionary *userInfo = noti.userInfo;
    AVAudioSessionRouteChangeReason reason = [userInfo[AVAudioSessionRouteChangeReasonKey] unsignedIntegerValue];
    if (reason == AVAudioSessionRouteChangeReasonOldDeviceUnavailable)
    {
        AVAudioSessionRouteDescription *previousRoute = userInfo[AVAudioSessionRouteChangePreviousRouteKey];
        AVAudioSessionPortDescription *previousOutput = previousRoute.outputs[0];
        NSString *portType = previousOutput.portType;
        if ([portType isEqualToString:AVAudioSessionPortHeadphones])
        {
            //原来使用的设备是耳机,现在不再使用耳机需要进行的操作
        }
    }
    
}

 

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