1

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

这个想法是,我在视图控制器中有 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)
            }
        }
    }
}

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

4

0 回答 0