gpt4 book ai didi

ios - AVCaptureSession 和 AVAudioSession 在背景音乐播放时录制视频只能工作一次

转载 作者:IT王子 更新时间:2023-10-29 05:52:19 25 4
gpt4 key购买 nike

看完这个问题:AVAudioSession AVAudioSessionCategoryPlayAndRecord glitch ,我试着尝试让背景音乐正常播放的视频录制。我正在解决录制开始和结束时的音频故障,并且第一次录制时它工作正常。但是如果我再次尝试录音,音乐就会停止。

有什么想法吗?

这是我的代码片段:

    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()

最佳答案

我正在做一个项目,我必须做几乎相同的事情:录制带有背景音频播放的视频。 (在 Xamarin 中)

在我的代码中,我在 onViewLoad 中设置了 AudioSession。

在将类别设置为 PlayAndRecord 之前,我还执行 audioSession.SetActive(false)。也许您应该在开始/停止录制时尝试这样做。

贝诺特

关于ios - AVCaptureSession 和 AVAudioSession 在背景音乐播放时录制视频只能工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46231219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com