I am coding a iOS apps with core audio. Something strange come across.
To do:
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *err = nil;
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDuckOthers error:&err];
[session setActive:YES error:&err];
I find everything is ok, background music is ducked. Then I try to resume it:
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&err];
With no error, but the back ground music is still ducked. Then I replace it with:
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:0 error:&err];
It works! Background music is gone!
I just don't understand why my first try fails and the second one successes?
In my view, there is no difference!
First of all, those two lines of code are not equivalent. AVAudioSessionCategoryOptionMixWithOthers
actually has an value of 1. Setting a value 0 means that you don't want any category options.
Second, it is unclear what you are actually trying to achieve. You say that you want to duck other audio channels (i.a. make them play with a lower volume) and then remove this effect (this would be setting the AVAudioSessionCategoryOptionMixWithOthers
option). But then you state your succes by the background music being gone, which is something else entirely.
EDIT
To remove the ducking effect try deactivating the ducking session in stead of setting a new category on it.
From the Apple documentation:
When ducking takes place, all other audio on the device—apart from phone audio—lowers in volume. Apps that use ducking must manage their session’s activation state. Activate the audio session prior to playing the audio and deactivate the session after playing the audio.