7

I have an audio app that uses the Media Playback audio session category to allow background audio. Once my audio session is initialized, the hardware volume buttons on the iOS device control the volume of my audio session.

Once audio playback stops, I'd like to return control of the phone ringer back to the hardware volume buttons but my attempt to do this by deactivating the audio session doesn't do the trick.

Here is how I'm initializing and activating my audio session:

AudioSessionInitialize(NULL, NULL, interruptionListenerCallback, self);

AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, 
                                audioRouteChangeListenerCallback, 
                                self);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;

AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
                        sizeof(sessionCategory),           
                        &sessionCategory);

Here is how I'm attempting to deactivate the audio session and return control of the iOS device's ringer back to the hardware volume controls:

AudioSessionSetActive(false);

There is at least one app that I know of that behaves this way (Audible.com's iOS app). Does anyone have any idea what I may be doing wrong?

Seth McFarland
  • 251
  • 1
  • 6
  • Did you try setting the category back to ambient? – reddersky Feb 03 '12 at 18:29
  • I tried setting the category to everything other than media playback with no luck. – Seth McFarland Mar 13 '12 at 11:31
  • Interesting, let me remind you that playing with iOS audio requires very careful error-handling. What is `OSStatus` returned by `AudioSessionSetActive(false);` ? http://developer.apple.com/library/ios/#DOCUMENTATION/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html – A-Live Mar 18 '12 at 16:30
  • The OSStatus returned by AudioSessionSetActive(false) is 560030580, which I believe is kAudioSessionNotActiveError ('!act'), which means "The audio operation failed because your application’s audio session was not active". However, using the volume controls at this time still controls the audio volume and not the device's ringer volume. – Seth McFarland Apr 11 '12 at 01:19

2 Answers2

0

In apples documentation I think you are going to have to actually remove the listener.

Look up: AudioSessionRemovePropertyListenerWithUserData

http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html

trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
  • I tried this out by calling the following with no luck: AudioSessionRemovePropertyListenerWithUserData(kAudioSessionProperty_AudioRouteChange, audioRouteChangeListenerCallback, self); – Seth McFarland Jul 12 '12 at 02:32
0

I just ran into this problem, but I'm using AVAudioPlayer. If I tried to deactivate my session right after calling play, it didn't work. But waiting for audioPlayerDidFinishPlaying:successfully: and then doing this worked:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error;
BOOL wasSuccessful = [audioSession setActive:NO error:&error];
NSLog(@"wasSuccessful: %@", wasSuccessful ? @"Yes" : @"No");
}

I'm using the default audio session, BTW.

Geoff Hom
  • 162
  • 1
  • 9