AudioServices.h not found in objective-C iOS project that includes AudioToolbox framework

南楼画角 提交于 2019-12-10 09:27:55

问题


According to the apple documentation, AudioServices.h should be part of the AudioToolbox framework.

Even though I have added the AudioToolbox framework to my Xcode project, when I #import AudioServices I get the error: AudioServices.h file not found.

This happens whether I type #import "AudioServices.h"

or #import "AudioToolbox/AudioServices.h" .

Just in case, I tried removing and then re-adding the AudioToolbox framework, which had no effect. Could the AudioServices file be corrupted somehow? (If so, does anyone know where I could download another copy?)

I am using XCode 4.2, but as I am converting some old open source code, the project is set to be XCode 3.2-compatible. Could this be the issue?

I'm sure I'm missing something simple. I am totally new to programming... any help appreciated!

-----EDIT (see my comment below)-----

in AudioServices.h, the two functions in question:

extern OSStatus
AudioSessionInitialize( CFRunLoopRef                        inRunLoop, 
                        CFStringRef                         inRunLoopMode, 
                        AudioSessionInterruptionListener    inInterruptionListener, 
                        void                                *inClientData)              

extern OSStatus
AudioSessionAddPropertyListener(    AudioSessionPropertyID              inID,
                                    AudioSessionPropertyListener        inProc,
                                    void                                *inClientData)

in SpeakHereController.mm (from sample Apple code), which I am trying to convert to ARC to get it to cooperate better with the other files in my project:

- (void)awakeFromNib
{       

// Allocate our singleton instance for the recorder & player object
recorder = new AQRecorder();
player = new AQPlayer();


OSStatus error = AudioSessionInitialize(NULL, NULL, interruptionListener, self);
if (error) printf("ERROR INITIALIZING AUDIO SESSION! %ld\n", error);
else 
{
    UInt32 category = kAudioSessionCategory_PlayAndRecord;  
    error = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
    if (error) printf("couldn't set audio category!");

    error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, propListener, self);
    if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %ld\n", error);
    UInt32 inputAvailable = 0;
    UInt32 size = sizeof(inputAvailable);

    // we do not want to allow recording if input is not available
    error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable);
    if (error) printf("ERROR GETTING INPUT AVAILABILITY! %ld\n", error);
    btn_record.enabled = (inputAvailable) ? YES : NO;

    // we also need to listen to see if input availability changes
    error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioInputAvailable, propListener, self);
    if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %ld\n", error);

    error = AudioSessionSetActive(true); 
    if (error) printf("AudioSessionSetActive (true) failed");
}

回答1:


The problem is that the system can't cast self to void * automatically when you're using ARC. Self is used in the last parameter of AudioSessionInitialize function (and others).

You need to tell ARC how the ownership of the memory is controlled by manually casting it to void * using __bridge. This says 'Don't change ownership of the memory'.

So change self to (__bridge void *)self in the calls to the AudioSession functions.

e.g.

OSStatus error = AudioSessionInitialize(NULL, NULL, interruptionListener, (__bridge void*)self);



回答2:


try "#import <AudioToolbox/AudioServices.h>" and see if your problem disappears.

The < and > characters make a difference. Using double quotes in an "#import" implies you want the compiler to search for "user headers", where the angled brackets imply you want the compiler to search in system frameworks.



来源:https://stackoverflow.com/questions/11662426/audioservices-h-not-found-in-objective-c-ios-project-that-includes-audiotoolbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!