观看视频时录制带音频的视频
Posted
技术标签:
【中文标题】观看视频时录制带音频的视频【英文标题】:Record video with audio when watching video 【发布时间】:2018-05-22 09:55:57 【问题描述】:当用户同时观看视频时,我正在尝试从前置摄像头录制视频。如果没有音频输入,源代码就像一个魅力,但是当我激活音频输入时,视频不会开始播放。这是可能的还是我正在努力实现不可能的事情?
记录视频源代码
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)
【问题讨论】:
这听起来很吓人。这是什么类型的应用程序? @kthorat 将成为一个革命性的应用程序。 ;-) 就像视频分享聊天一样。类似于 Skype,但没有流式传输视频。我要收藏 太棒了!请注意用户的隐私,并通知用户有关录制和其他活动的信息。祝你好运。 @kthorat 不用担心。一切都会合法 【参考方案1】:问题在于线程处理。
我的解决办法是:
func playerReadyToPlay()
DispatchQueue.global(qos: .userInitiated).async
self.player?.play()
super.startRecording()
【讨论】:
以上是关于观看视频时录制带音频的视频的主要内容,如果未能解决你的问题,请参考以下文章
与 Internet Explorer 兼容的带音频录制的视频