Return volume control to iOS device ringer after playback ends
Asked Answered
K

2

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?

Kodok answered 20/11, 2011 at 17:26 Comment(4)
Did you try setting the category back to ambient?Exoteric
I tried setting the category to everything other than media playback with no luck.Kodok
Interesting, let me remind you that playing with iOS audio requires very careful error-handling. What is OSStatus returned by AudioSessionSetActive(false); ? developer.apple.com/library/ios/#DOCUMENTATION/AudioToolbox/…Godforsaken
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.Kodok
N
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

Neurocoele answered 30/5, 2012 at 20:44 Comment(1)
I tried this out by calling the following with no luck: AudioSessionRemovePropertyListenerWithUserData(kAudioSessionProperty_AudioRouteChange, audioRouteChangeListenerCallback, self);Kodok
F
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.

Fungosity answered 31/8, 2013 at 7:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.