检测耳机的插入与拔出

我的技术博客今天终于正式开通啦!仅以这篇博客,纪念项目的结尾,同时也是我技术博客生涯的开端!废话少说,直接上代码。


    AudioSessionInitialize (NULL,NULL,NULL,NULL);

    UInt32 sessionCategory =kAudioSessionCategory_MediaPlayback;

    AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,sizeof (sessionCategory), &sessionCategory);

    // Added Detecting pluging in /out the headphones

    AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, RouteChangeListener,self);

    AudioSessionSetActive(true);


void RouteChangeListener(void *inClientData,AudioSessionPropertyIDinID, UInt32 inDataSize, const void *inData) {

//NSLog(@"Headphones in or out!!!!!!");

UInt32 dataSize;

CFStringRef currentRoute;

currentRoute =NULL;

dataSize =sizeof(CFStringRef);

AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &dataSize, &currentRoute);

NSLog(@"Return String:%@",currentRoute);

if([(NSString *) currentRoutehasPrefix: @"Headphone"]){

NSLog(@"Headphone!!");

}else {

NSLog(@"Speaker!!");

}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值