问题描述
在查看了以下问题之后: AVAudioSession AVAudioSessionCategoryPlayAndRecord故障,我试图阻止试图正常播放背景音乐的视频录制.录制开始时和录制结束时,我都为音频故障做好了准备,并且在第一次录制时效果很好.但是,如果我尝试再次录制,音乐将停止.
After looking at this question: AVAudioSession AVAudioSessionCategoryPlayAndRecord glitch, i tried to take a stab at trying to get video recording with background music playing working correctly. I'm settling for the audio glitch when recording starts, and when it ends, and it works fine the first time the recording happens. But if I try to record again, the music will stop.
有什么想法吗?
这是我的代码的片段:
captureSession = AVCaptureSession()
captureSession?.automaticallyConfiguresApplicationAudioSession = false
captureSession?.usesApplicationAudioSession = true
guard let captureSession = self.captureSession else {
print("Error making capture session")
return;
}
captureSession.sessionPreset = AVCaptureSessionPresetHigh
self.camera = self.defaultBackCamera()
self.audioDeviceInput = try? AVCaptureDeviceInput(device: getAudioDevice())
cameraInput = try AVCaptureDeviceInput(device: camera)
captureSession.beginConfiguration()
if captureSession.inputs.count > 0 {
return
}
if captureSession.canAddInput(cameraInput) {
captureSession.addInput(cameraInput)
if captureSession.outputs.count == 0 {
photoOutput = AVCapturePhotoOutput()
if captureSession.canAddOutput(photoOutput!) {
captureSession.addOutput(self.photoOutput!)
}
}
captureSession.commitConfiguration()
if !captureSession.isRunning {
captureSession.startRunning()
self.previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
self.previewLayer!.videoGravity = AVLayerVideoGravityResizeAspect
self.previewLayer!.connection.videoOrientation = .portrait
self.previewLayer!.frame = cameraView.layer.bounds
self.cameraView.layer.addSublayer(self.previewLayer!)
captureSession.beginConfiguration()
videoFileOut = AVCaptureMovieFileOutput()
if (captureSession.canAddOutput(videoFileOut)) {
captureSession.addOutput(videoFileOut)
if (videoFileOut?.connection(withMediaType: AVMediaTypeVideo).isVideoStabilizationSupported)! {
videoFileOut?.connection(withMediaType: AVMediaTypeVideo).preferredVideoStabilizationMode = .cinematic
}
}
captureSession.commitConfiguration()
}
这是开始记录的代码:
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord,
with: [.mixWithOthers, .allowBluetoothA2DP, .allowAirPlay])
try! audioSession.setActive(true)
captureSession?.beginConfiguration()
if (captureSession?.canAddInput(audioDeviceInput!))! {
captureSession?.addInput(audioDeviceInput!)
}
captureSession?.commitConfiguration()
并停止记录:
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setCategory(AVAudioSessionCategoryAmbient, with: [.mixWithOthers, .allowAirPlay])
captureSession?.beginConfiguration()
captureSession?.removeInput(audioDeviceInput)
captureSession?.commitConfiguration()
推荐答案
在我看来,这与您的AVAudioSession
有关.停止录制时,不要将audioSession设置为非活动状态.当您开始新的录制时更改AVAudioSession
类别时,这将导致立即更改路由.
It seems to me this has to do with your AVAudioSession
. You do not set the audioSession to inactive when you are stopping the recording. This results in an immediate route change when you change the AVAudioSession
category when starting a new recording.
尝试在停止录制时将AVAudioSession
设置为非活动状态.
Try setting the AVAudioSession
inactive when stopping the recording.
不同的AVAudioSession
类别对AirPlay的影响也不同.
Also different AVAudioSession
categories have different impact on AirPlay.
从
https://developer.apple.com/documentation/avfoundation/avaudiosession/audio_session_categories
"音频会话类别AVAudioSessionCategoryPlayAndRecord仅支持AirPlay的镜像变体"和
"仅音频会话播放类别(AVAudioSessionCategoryAmbient,AVAudioSessionCategorySoloAmbient和AVAudioSessionCategoryPlayback)支持AirPlay的镜像和非镜像变体."
我建议在Apple文档中对AVAudioSession
进行一些阅读,因为有时只需要认真地考虑控制AVAudioSession
本身.
I recommend a little reading on AVAudioSession
in the Apple docs since just controlling the AVAudioSession
itself sometimes need to be really well thought-out.
https://developer.apple.com/documentation/avfoundation/avaudiosession
这篇关于AVCaptureSession和AVAudioSession录制视频,而播放背景音乐仅工作一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!