Я пытаюсь записать видео с фронтальной камеры, когда пользователь просматривает видео.Без аудиовхода исходный код работает как брелок, но когда я активирую аудиовход, видео не начинает воспроизводиться.Возможно ли это, или я пытаюсь достичь чего-то невозможного?
ЗАПИСАТЬ КОД ВИДЕОИСТОЧНИКА
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)
}