I m working on iPad.
I would like to detect when user plug-out headphone. First I used a listener on the property kAudioSessionProperty_AudioRouteChange
. So all was working well until I ve decided to add a button to switch to speakers when headphone was still plugged. So I’m now facing a problem, maybe someone would have an idea to fix it.
这里的情况是:
- I plug a headphone -> my audio route change callback is called
- then I switch sound to speakers (without unplugging my headphone) -> audio route change callback is called
- then I unplug my headphone (when sound is still outputting to speakers) -> audio route change callback is NOT called, which seems logical.
但我的问题是: 因此,我的问题是: 你们是否看到了一种办法,可以发现这名头盔没有被打上最后一件事。
感谢您的帮助
http://www.un.org。
Ok I发现了一个工作:
为了发现耳光是否被封不动,我在我需要知道的任何时候(而不是使用ean子)都履行测试职能,这或许对业绩不太好,但对于工作来说却不太好。
//set back the default audio route
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);
//check if this default audio route is Heaphone or Speaker
CFStringRef newAudioRoute;
UInt32 newAudioRouteSize = sizeof(newAudioRoute);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &newAudioRouteSize, &newAudioRoute);
NSString *newAudioRouteString = (__bridge NSString *)newAudioRoute;
CFRelease(newAudioRoute);
//if this default audio route is not Headphone, it means no headphone is plugged
if ([newAudioRouteString rangeOfString:@"Headphones"].location != NSNotFound){
NSLog(@"Earphone available");
return true;
}
else {
NSLog(@"No Earphone available");
return false;
}
希望能帮助人们!