简体   繁体   English

AVAudioSession setCategory成功但没有任何反应

[英]AVAudioSession setCategory success but nothing happens

I am coding a iOS apps with core audio. 我正在使用核心音频编写iOS应用程序。 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 . AVAudioSessionCategoryOptionDuckOthersAVAudioSessionCategoryOptionMixWithOthers0之间存在差异。

AVAudioSessionCategoryOptionMixWithOthers : Mixes audio from this session with audio from other active sessions. AVAudioSessionCategoryOptionMixWithOthers :将此会话中的音频与来自其他活动会话的音频混合。

AVAudioSessionCategoryOptionDuckOthers : Causes audio from other sessions to be ducked (reduced in volume) while audio from this session plays. AVAudioSessionCategoryOptionDuckOthers :当来自此会话的音频播放时,导致来自其他会话的音频被AVAudioSessionCategoryOptionDuckOthers (音量减小)。

And 0 means no options. 0表示没有选项。

So, other options require another session to perform task. 因此,其他选项需要另一个会话来执行任务。

For more details about options of session refer Apple documentation . 有关会话选项的更多详细信息,请参阅Apple文档

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. AVAudioSessionCategoryOptionMixWithOthers实际上具有值1.设置值0意味着您不需要任何类别选项。

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). 你说你想要躲避其他音频通道(让它们以较低音量播放)然后删除此效果(这将设置AVAudioSessionCategoryOptionMixWithOthers选项)。 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: 从Apple文档:

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. 使用ducking的应用必须管理其会话的激活状态。 Activate the audio session prior to playing the audio and deactivate the session after playing the audio. 在播放音频之前激活音频会话,并在播放音频后停用会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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