我使用此代码检查我的iphone的audiojack中是否有插入或插入的东西
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");
}
}
}
任何建议都将受到赞赏。
答案 0 :(得分:1)
audiotest *test;
您似乎尚未初始化test
。尝试将其替换为:
audiotest *test = [[audiotest alloc] init];
另外作为旁注,它是PascalCase的一个Objective-C约定你的类名和camelCase你的方法名。例如:
AudioTest *test = [[AudioTest alloc] init];
int status = [test checkHeadphoneStatus];