2

我使用了这些代码

- (void)viewDidLoad
{
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
AudioSessionAddPropertyListener (
                                 kAudioSessionProperty_AudioRouteChange,
                                 audioRouteChangeListenerCallback,
                                 self
                                 );
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];
}

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
        } else if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable) {
            //Handle Headset plugged in
        }

    }
}

检测我的项目中是否插入或拔出音频插孔,但最终出现此错误

Undefined symbols for architecture armv7:
  "_AVAudioSessionCategoryAmbient", referenced from:
      -[ViewController viewDidLoad] in ViewController.o
  "_OBJC_CLASS_$_AVAudioSession", referenced from:
      objc-class-ref in ViewController.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

那么其中的问题是什么?我是 ios 的初学者,任何帮助将不胜感激。

4

1 回答 1

17

您错过了添加AudioToolBox.Frameworks到项目文件中。仔细检查您的项目文件设置..

更新 实际上错过了 AVFoundation.Frameworks 在项目的构建阶段添加。

更新 2
尾随s已被删除。现在是AudioToolBox.FrameworkAVFoundation.Framework

于 2013-04-23T06:13:09.267 回答