Max*_*rbe 14 objective-c ios avcapturesession avcapturedevice avaudiosession
每当我启动以麦克风作为输入运行的AVCaptureSession时,它都会取消当前正在运行的任何背景音乐(例如iPod音乐).如果我注释掉添加音频输入的行,背景音频将继续.
有没有人知道用麦克风录制视频剪辑的方法,同时继续允许播放背景音频?当您尝试录制视频并且当前正在播放音乐时,也会出现错误.
试图这样做:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
Run Code Online (Sandbox Code Playgroud)
但 'AudioSessionSetProperty' is deprecated: first deprecated in iOS 7.0
所以我试着这样做:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:&setCategoryError];
[audioSession setActive:YES error:nil];
Run Code Online (Sandbox Code Playgroud)
但最后它没有奏效.感谢帮助!
Gab*_*ana 28
在你的AppDelegate中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Stop app pausing other sound.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionDuckOthers | AVAudioSessionCategoryOptionDefaultToSpeaker
error:nil];
}
Run Code Online (Sandbox Code Playgroud)
你在哪里分配AVCaptureSession:
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.automaticallyConfiguresApplicationAudioSession = NO;
Run Code Online (Sandbox Code Playgroud)
此代码将允许您播放背景音乐并AVCaptureSession
使用麦克风运行.
SWIFT更新:
AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//Stop app pausing other sound.
do{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord,
withOptions: [.DuckOthers, .DefaultToSpeaker])
}
catch {
}
return true
}
Run Code Online (Sandbox Code Playgroud)
你在哪里分配AVCaptureSession:
let session = AVCaptureSession()
session.automaticallyConfiguresApplicationAudioSession = false
Run Code Online (Sandbox Code Playgroud)
KNa*_*ito 18
你可以使用AVAudioSessionCategoryOptionMixWithOthers
.例如,
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
Run Code Online (Sandbox Code Playgroud)
之后,您可以AVAudioPlayer
同时使用AVCaptureSession
.
但是,上述代码导致音量非常低.如果您想要正常音量,请使用AVAudioSessionCategoryOptionDefaultToSpeaker
with AVAudioSessionCategoryOptionMixWithOthers
,如下所示,
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
Run Code Online (Sandbox Code Playgroud)
这很顺利.