-1

我使用此代码检查是否在我的 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");
            }
                    }

        }

任何建议将不胜感激。

4

1 回答 1

1
audiotest *test;

看起来你还没有初始化test。尝试将其替换为:

audiotest *test = [[audiotest alloc] init];

另外作为旁注,这是一个Objective-C约定,PascalCase你的类名和camelCase你的方法名。例如:

AudioTest *test = [[AudioTest alloc] init];
int status = [test checkHeadphoneStatus];
于 2013-05-01T08:04:31.227 回答