Audio Session Property Listener auto-deactivated in UIImagePickerController?
I'm developing a camera app to snap photo when the volume button is开发者_Python百科 pressed.
I used AudioSessionAddPropertyListener(kAudioSessionProperty_CurrentHardwareOutputVolume, audioVolumeChangeListenerCallback, self);
to successfully get notified when a volume button get pressed before my camera started. However, after I started the camera, this property listener no longer works. Is it get auto deactivated or something?
I have tried to implement a custom UIImagePickerController to include the Audio Session Property Listener inside the ImagePickerController but with no hope. Grateful if anyone can share your view. Thanks.
I'm sorry to say that this is something that is not supported in IOS4. If you managed to implement this successfully the app will get rejected by apple.
However is was announced at WWDC 2011 that this is fully supported in IO5 and will be default behaviour when using UIImagePickerController in any app.
Hope this helps.
Even in the default camera app on the iphone, turning up/down the volume is not supported. May be they block the input from those buttons when the camera is turned on. What they dont block is the silent switch. I added this notification and my callback was called even with the camera on:
AudioSessionInitialize(nil, nil, nil, nil);
AudioSessionSetActive(true);
AudioSessionAddPropertyListener(
kAudioSessionProperty_AudioRouteChange,
applicationAudioRouteDidChange,
self);
The problem you need to solve is that the 'applicationAudioRouteDidChange' method will also be called if someone inserts the headphones. You can check this as said in the AudioSessionProgrammingGuide:
When the system invokes a route-change callback, it provides the information you need to figure out which action to take. Base your callback on the AudioSessionPropertyListener prototype from audio session services, as shown here:
void MyPropertyListener ( void *inClientData, AudioSessionPropertyID inID,UInt32 inDataSize, const void *inData );
For a route change event, the system sends the kAudioSessionProperty_AudioRouteChange in the inID parameter.
The inData parameter sent to your callback contains a CFDictionaryRef object that describes:
Why the route changed What the previous route was
I did this and it takes the picture when silent switch is mmmm switched:
void applicationAudioRouteDidChange(void *inClientData,
AudioSessionPropertyID inID,
UInt32 inDataSize, const void *inData)
{
if ([[(NSDictionary*)inData objectForKey:@"OutputDeviceDidChange_Reason"] intValue] == 5) {
[((RootViewController*)inClientData).picker takePicture];
}
// Do something like reset the system
}
As mentioned by Tom, the functionality you are trying to implement would be a part of iOS 5.
精彩评论