简体   繁体   中英

AVAudioSession setCategory success but nothing happens

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!

You have set different options in every try. there is a difference between AVAudioSessionCategoryOptionDuckOthers and AVAudioSessionCategoryOptionMixWithOthers and 0 .

AVAudioSessionCategoryOptionMixWithOthers : Mixes audio from this session with audio from other active sessions.

AVAudioSessionCategoryOptionDuckOthers : Causes audio from other sessions to be ducked (reduced in volume) while audio from this session plays.

And 0 means no options.

So, other options require another session to perform task.

For more details about options of session refer Apple documentation .

Hope this will help :)

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 (ia 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM