0

I am adding an Audio session property listener for kAudioSessionProperty_AudioRouteChange and in the callback I want to call the takePicture function of UIImagePickerController. The problem is that I cannot access my picker in the callback.

I am initializing my picker in viewDidLoad. I have tried declaring the picker as both a private and public object and accessing with picker or self.picker but it always gives the "Use of undeclared identifier" error. I use this code to add the listener:

AudioSessionInitialize(nil, nil, nil, nil);
AudioSessionSetActive(true);
AudioSessionAddPropertyListener(
                                kAudioSessionProperty_AudioRouteChange,
                                applicationAudioRouteDidChange,
                                self);

This is my callback:

void applicationVolumeDidChange(void *inClientData,
                                AudioSessionPropertyID inID,
                                UInt32 inDataSize, const void *inData)
{
    NSLog(@"Volume changed");
    //[picker takePicture]; Error

}

I also declared an NSArray to see if this was the problem with UIImagePickerController only but the array also gives the same error.

Bushra Shahid
  • 3,579
  • 1
  • 27
  • 37

2 Answers2

2

The last parameter of AudioSessionAddPropertyListener() is there so you can pass whatever you like into the callback.

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

You are passing self so within the callback the void *inClientData parameter is a pointer to whichever object self is.

If self was an instance of UIImagePickerController then,

UIImagePickerController *picker = inClientData;
[picker takePicture];
Bushra Shahid
  • 3,579
  • 1
  • 27
  • 37
hooleyhoop
  • 9,128
  • 5
  • 37
  • 58
  • self is my view controller so in my case i use `UIImagePickerController *myPicker = (RootViewController*)inClientData.picker;` Your direction was correct though. – Bushra Shahid Aug 03 '11 at 07:07
1

Your callbacks must be C functions, not Objective-C methods. These are not compatible. See this link.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94