Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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");
}
like image 610
Jeanne Avatar asked Dec 04 '22 02:12

Jeanne


1 Answers

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);
like image 189
Ben Clayton Avatar answered Dec 30 '22 09:12

Ben Clayton