检测耳机的插入与拔出

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


    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!!");

}

}




你可能感兴趣的:(String,null)