</pre>参考文章<p></p><p>http://blog.csdn.net/like7xiaoben/article/details/8990730</p><p>http://www.2cto.com/kf/201410/347849.html (确定使用此文章)</p><p>两篇文章大意 总体大家ios7之前大部分使用c的实现方式,ios7以后给废弃了,建议使用高级的api</p><p>今天我百般无奈,使用了c的方式,结果不好使,改用api方式,简单直接上代码</p><p></p><pre name="code" class="objc">//添加通知,拔出耳机后暂停播放
[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(routeChange:) name:AVAudioSessionRouteChangeNotification object:nil];
接下来实现方法
/**
* 一旦输出改变则执行此方法
*
* @param notification 输出改变通知对象
*/
-(void)routeChange:(NSNotification *)notification{
NSDictionary *dic=notification.userInfo;
int changeReason= [dic[AVAudioSessionRouteChangeReasonKey] intValue];
//等于AVAudioSessionRouteChangeReasonOldDeviceUnavailable表示旧输出不可用
if (changeReason==AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
AVAudioSessionRouteDescription *routeDescription=dic[AVAudioSessionRouteChangePreviousRouteKey];
AVAudioSessionPortDescription *portDescription= [routeDescription.outputs firstObject];
//原设备为耳机则暂停
if ([portDescription.portType isEqualToString:@"Headphones"]) {
//juanqiang 这里写你要暂停的操作
}
}
}
使用完毕,卸载掉监听
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVAudioSessionRouteChangeNotification object:nil];
}
简简单单,一个注册,一个实现,最后别忘了卸载监听,希望对你们有帮助,记得分享。