Audio Session “Ducking” Broken in iOS 4…?

风流意气都作罢 提交于 2019-12-03 13:08:19
zaphodtx

I used this post when I was having a similar issue and had trouble getting it to work consistently. It would work for a while and then just get stuck "ducked". I spent a lot of time researching and debugging this and finally just called Apple. They told me to look at the breadcrumb sample code. I followed that example and everything worked fine.

Here is Apple's sample code:

http://developer.apple.com/library/ios/#samplecode/Breadcrumb/Introduction/Intro.html

The support/developer at Apple also said to watch the order of how and when they set the session properties. That apparently was the trick. After reading a lot of posts here and elsewhere, I had settings that conflicted with each other. Starting from scratch and following the bread crumb example made it work. I have not had an issue since.

I posted the same answer here:

How to unduck AVAudioSession

To me this answer was borderline trade secret since it was so difficult to get working and there were working apps in the market. For me, seeing a working example, which was pretty much just the sound ducking and unducking was worth its weight in gold.

Also, I mentioned to Apple that this was a known issue and they did not agree. They were not aware of any issue here. Sure enough if you follow the breadcrumb example it works with out any strange session deactivate and reactive, etc., etc.

Ducking is not automatically related to your playing sounds in any way. When you turn on ducking, background sound ducks. When you turn off ducking and deactivate your audio session, background sound comes back to its original level.

So don't turn on ducking in your viewDidLoad. Don't turn it on until you are actually about to play the sound. That causes the background sound to duck. When your sound is done playing, turn ducking back off and toggle your audio session inactive and active again. That causes the background sound to unduck:

UInt32 duck = 0;
AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck, 
                        sizeof(duck), &duck);
AudioSessionSetActive(false);
AudioSessionSetActive(true);

The documentation isn't clear about what the expected behavior of ducking is supposed to be, but what I've found is that if you set up your audio session for ducking, when you activate the audio session, it ducks, and when you deactivate it, it unducks. So you have to keep activating and deactivating the audio session to duck the music just for your sound cue.

One bug I found is that if you deactivate the audio session while inside the audioPlayerDidFinishPlaying:successfully: method of your AVAudioPlayerDelegate, the ducking isn't removed, and the volume is kept at the same level. I've already filed a Radar on this, but it doesn't hurt if other file similar ones. In fact it'll probably put some pressure on them to fix it.

What you're doing is starting the audio session and keeping it on, so it ducks immediately and stays ducked, so that it won't duck the music any further when you play a sound. The issue with the music volume going back up after pausing and playing the music sounds like a bug.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!