AVCaptureSession和背景音频iOS 7 [英] AVCaptureSession and background audio iOS 7

查看:286
本文介绍了AVCaptureSession和背景音频iOS 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我开始使用麦克风作为输入运行AVCaptureSession时,它会取消当前正在运行的任何背景音乐(例如iPod音乐)。如果我注释掉添加音频输入的行,则背景音频会继续。

Whenever I start an AVCaptureSession running with the microphone as an input it cancels whatever background music is currently running (iPod music for instance). If I comment out the line adding the audio input, the background audio continues.

有没有人知道用麦克风录制视频片段的方法,同时继续允许背景音频播放?当您尝试录制视频并且当前正在播放音乐时也会出现错误。

A试图这样做:

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];

但是 'AudioSessionSetProperty'已被弃用:首先在iOS 7.0中弃用

所以我尝试这样做:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil; 
[audioSession setCategory:AVAudioSessionCategoryPlayback
              withOptions:AVAudioSessionCategoryOptionMixWithOthers
                    error:&setCategoryError];
[audioSession setActive:YES error:nil];

但最后它无效。感谢您的帮助!

But finally it didn't work. Thanks for help!

推荐答案

您可以使用 AVAudioSessionCategoryOptionMixWithOthers
例如,

You can use the AVAudioSessionCategoryOptionMixWithOthers. For instance,

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];

之后,您可以使用 AVAudioPlayer 同时使用 AVCaptureSession

After that, you can use the AVAudioPlayer simultaneously with AVCaptureSession.

但是,上述代码导致音量非常低。
如果你想要正常音量,请使用 AVAudioSessionCategoryOptionDefaultToSpeaker AVAudioSessionCategoryOptionMixWithOthers ,如下所示,

However, the above code leads to very low volume. If you want normal volume, use the AVAudioSessionCategoryOptionDefaultToSpeaker with AVAudioSessionCategoryOptionMixWithOthers as follows,

[session setCategory:AVAudioSessionCategoryPlayAndRecord  withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];

这很顺利。

这篇关于AVCaptureSession和背景音频iOS 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆