1

I am working with an application which requires both playing video (from external source) and show self image.

The idea is, I have 2 views in the viewcontroller where 1 is for playing video and 1 is for self image.

Currently, I can get self image playing by using front camera. However, my video does not play at all.

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

Anyone please helps me figure out why my video is not playing. Thanks.

Le Trong Triet
  • 101
  • 1
  • 10

0 Answers0