gpt4 book ai didi

ios - 观看视频时录制视频并带有音频

转载 作者:行者123 更新时间:2023-11-30 11:28:50 26 4
gpt4 key购买 nike

当用户同时观看视频时,我尝试从前置摄像头录制视频。如果没有音频输入,源代码就像一个魅力,但是当我激活音频输入时,视频不会开始播放。这是可能的还是我正在尝试实现不可能的目标?

录制视频源代码

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

self.session.beginConfiguration()
self.session.sessionPreset = AVCaptureSessionPresetMedium

// Add video input.
do {
guard let videoDevice = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front) else {fatalError()}
let videoDeviceInput = try AVCaptureDeviceInput(device: videoDevice)

if self.session.canAddInput(videoDeviceInput) {
self.session.addInput(videoDeviceInput)
} else {
print("Could not add video device input to the session")
self.session.commitConfiguration()
return
}
} catch {
print("Could not create video device input: \(error)")
self.session.commitConfiguration()
return
}

// Add audio input.
do {
guard let audioDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio) else {fatalError()}
let audioDeviceInput = try AVCaptureDeviceInput(device: audioDevice)

if self.session.canAddInput(audioDeviceInput) {
self.session.addInput(audioDeviceInput)
}
else {
print("Could not add audio device input to the session")
}
} catch {
print("Could not create audio device input: \(error)")
}

self.videoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.session)
self.videoPreviewLayer!.videoGravity = AVLayerVideoGravityResizeAspect
self.videoPreviewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
self.cameraElement.layer.addSublayer(self.videoPreviewLayer!)
self.session.commitConfiguration()
self.session.startRunning()
}

func startRecording() {
let recordingDelegate: AVCaptureFileOutputRecordingDelegate? = self
self.videoFileOutput = AVCaptureMovieFileOutput()
self.session.addOutput(videoFileOutput)

let filePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("tmpVideo.mov")
ContentController.tmpFilePath = filePath
videoFileOutput?.startRecording(toOutputFileURL: filePath, recordingDelegate: recordingDelegate)

}

播放视频源代码

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

player = AVPlayer(url: ContentController.content!.url!)
let playerLayer: AVPlayerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.videoElement.bounds
self.videoElement.layer.addSublayer(playerLayer)

player?.currentItem!.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions(), context: nil)
}

最佳答案

问题出在线程处理上。

我的解决方案是:

func playerReadyToPlay() {
DispatchQueue.global(qos: .userInitiated).async {
self.player?.play()
}
super.startRecording()
}

关于ios - 观看视频时录制视频并带有音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50464782/

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