Knowing when headphones are connected to the 3.5 mm jack works only if they weren't connected before the app started
I got this code to tell if the headphones are connected or not:
This in viewDidLoad:
AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, audioSessionPropertyListener, nil);
This function:
void audioSessionPropertyListener(void* inClientData, AudioSessionPropertyID inID,
UInt32 inDataSize, const void* inData) {
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
// Determines the reason for the route change, to ensure that it is not
// because of a category change.
CFDictionaryRef routeChangeDictionary = inData;
CFNumberRef routeChangeReasonRef = CFDictionaryGetValue (routeChangeDictionary,CFSTR (kAudioSession_AudioRouteChangeKey_Reason));
SInt32 routeChangeReason;
CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);
// "Old device unavailable" indicates that a headset was unplugged, or that the
// device was removed from a dock connector that supports audio output.
if (routeChangeReason != kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
[[Cubic_01AppDelegate sharedInstance] setDongleIsDisonnected];
}
if (!isHeadsetPluggedIn())
{
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
}
else
{
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);
}
}
and this fu开发者_高级运维nction:
BOOL isHeadsetPluggedIn() {
UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;
OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
&routeSize,
&route
);
NSLog(@"%@", route);
BOOL isPluggedIn = !error && (route != NULL) && ([(NSString*)route rangeOfString:@"Head"].location != NSNotFound);
if (isPluggedIn) {
[[Cubic_01AppDelegate sharedInstance] setDongleIsConnected];
} else {
[[Cubic_01AppDelegate sharedInstance] setDongleIsDisonnected];
}
return (isPluggedIn);
}
Can anyone please help with telling me why the audioSessionPropertyListener function isn't invoked when the headphones are already connected before the app starts?
You need to provide your check on load/startup of the application. Basically you should be able to call:
- (void) viewDidLoad
{
isHeadsetPluggedIn()
}
The property listener has not been set if you haven't started your application before connecting the headphones. Note that the property listener is checking for changes only.
So you will need to provide your check on load/startup of the application. You can get the AudioSession properties directly without using a listener and thus parse. And that is exactly what you do in isHeadsetPluggedIn() as you call:
OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
&routeSize,
&route);
This method can be called in objective c and c++ functions alike. It can be called anywhere to get any property of the AudioSession after it has been initialized.
精彩评论