无法同时播放视频和显示捕获

时间:2017-08-08 07:29:02

标签: ios swift avfoundation avcapturesession avkit

我正在使用一个需要播放视频(来自外部源)并显示自我图像的应用程序。

我的想法是,我在viewcontroller中有2个视图,其中1个用于播放视频,1个用于自我图像。

目前,我可以使用前置摄像头进行自我影像播放。但是,我的视频根本不播放。

func whatever() {

    let deviceSession = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInDuoCamera,.builtInTelephotoCamera,.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .unspecified)

    for device in (deviceSession?.devices)! {
        if device.position == AVCaptureDevicePosition.front {
            do {
                let input = try AVCaptureDeviceInput(device: device)
                if captureSession.canAddInput(input){
                    captureSession.addInput(input)
                    if captureSession.canAddOutput(sessionOutput){
                        captureSession.addOutput(sessionOutput)

                        if MyVariables.testCallTime != 0.0 {
                            let seekTime: CMTime = CMTimeMakeWithSeconds(Float64(MyVariables.testCallTime), 1)
                            player.seek(to: seekTime)
                        }

                        playerLayer = AVPlayerLayer(player: player)
                        self.videoView.layer.addSublayer(playerLayer)
                        playerLayer.position = CGPoint (x: self.videoView.frame.width / 2, y: self.videoView.frame.height / 2)
                        playerLayer.bounds = self.videoView.frame

                        self.player.play()

                        NotificationCenter.default.addObserver(self, selector: (#selector(playTestCallViewController.videoDone)),name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)

                        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                        previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
                        previewLayer?.connection.videoOrientation = .landscapeRight
                        cameraView.layer.addSublayer(previewLayer!)
                        previewLayer?.position = CGPoint (x: self.cameraView.frame.width / 2, y: self.cameraView.frame.height / 2)
                        previewLayer?.bounds = cameraView.frame

                        self.captureSession.startRunning()
                    }
                }
            } catch let avError {
                print(avError)
            }
        }
    }
}

任何人请帮我弄清楚为什么我的视频没有播放。感谢。

0 个答案:

没有答案