我使用此代码检查是否在我的 iphone 的音频插孔中插入或拔出某些东西
void audioRouteChangeListenerCallback (void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize, const void *inPropertyValue ) {
// ensure that this callback was invoked for a route change
if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return;
{
// Determines the reason for the route change, to ensure that it is not
// because of a category change.
CFDictionaryRef routeChangeDictionary = (CFDictionaryRef)inPropertyValue;
CFNumberRef routeChangeReasonRef = (CFNumberRef)CFDictionaryGetValue (routeChangeDictionary, CFSTR (kAudioSession_AudioRouteChangeKey_Reason) );
SInt32 routeChangeReason;
CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);
if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
//Handle Headset Unplugged
NSLog(@"PluggedOut");
}
else if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable)
{
//Handle Headset plugged in
NSLog(@"Something Plugged In");
}
}
但是当我添加我的目标 C 方法(在所有其他代码中都可以完美运行)以确定插入的设备是否是我的读卡器时,它最终会出现 EXC_BAD_ACCESS 错误,我尝试的是下面的代码
else if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable)
{
//Handle Headset plugged in
NSLog(@"Something Plugged In");
audiotest *test;
if([test Checkheadphonestatus] == 1)
{
NSLog(@"Your iPosReader Connected");
}
else if([test Checkheadphonestatus] == 0)
{
NSLog(@"Connected device was not iPos Reader");
}
}
}
任何建议将不胜感激。