开发者

How to mute background audio while recording (using Audio Queue Services)

开发者 https://www.devze.com 2023-02-07 21:38 出处:网络
I have an application which uses Audio Queues to do recording of spoken user input. This only happens for short durations and only when the user presses a button.

I have an application which uses Audio Queues to do recording of spoken user input. This only happens for short durations and only when the user presses a button.

I'd like to allow the user's background music to continue playing, except have it muted or playing at a much lower volume whenever the recording is actually taking place (to avoid recording the audio playing o开发者_运维知识库ut of the speaker).

For my recording queue, I'm using the PlayAndRecord category, and I have the OverrideCategoryMixWithOthers property set to true.


This problem became less of an issue in iOS4+, when they added the AudioSessionSetActiveWithFlags API and the kAudioSessionSetActiveFlag_NotifyOthersOnDeactivation flag.

To pause background music while recording, you need to call AudioSessionSetActive(false), which will deactivate your audio session, then switch categories to PlayAndRecord mode, and set the OverrideCategoryMixWithOthers property to false.

Now, reactivate your session with AudioSessionSetActive(true) with these settings, the audio session for iPod/Pandora/etc will be interrupted and will fade-out and pause.

Then, when your recording is finished, deactivate your session again using

AudioSessionSetActiveWithFlags(false, kAudioSessionSetActiveFlag_NotifyOthersOnDeactivation);

This will notify the background music apps that their session is no longer interrupted and they can begin playing music.

Finally, you can set up your audio session again using MediaPlayback (or equiv.) mode and setting the OverrideCategoryMixWithOthers property to true again.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号