如何播放录制的视频?

时间:2017-07-18 21:01:45

标签: swift swift3 avfoundation

这是我尝试播放的视频文件类型的示例: 文件:///private/var/mobile/Containers/Data/Application/7725BCCA-B709-48FB-8FE3-DBC9F4B679C0/tmp/9AD6A48E-6A25-4114-88D3-474A0E1C762F.mp4

我认为它是录音,但当我尝试播放录制的视频时,它只是一个空白屏幕。

func startRecording() {

        print("start")
        if movieOutput.isRecording == false {

            let connection = movieOutput.connection(withMediaType: AVMediaTypeVideo)

            if (connection?.isVideoOrientationSupported)! {

                connection?.videoOrientation = currentVideoOrientation()
            }

            if (connection?.isVideoStabilizationSupported)! {
                connection?.preferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.auto
            }
            print(AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo))


            let device = activeInput.device
            if (device?.isSmoothAutoFocusSupported)! {

                do {
                    try device?.lockForConfiguration()
                    device?.isSmoothAutoFocusEnabled = false
                    device?.unlockForConfiguration()

                } catch {
                    print("Error setting configuration: \(error)")
                }

            }

            outputURL = tempURL()
            movieOutput.startRecording(toOutputFileURL: outputURL, recordingDelegate: self)



        } else {
            print("stop")
            stopRecording()
        }

    }


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showPreview" {

            let previewVC = segue.destination as! ProfilePhotoPreviewViewController
            previewVC.image = self.image

        } else if segue.identifier == "showVideoPreview" {
            let vc = segue.destination as! ProfilePhotoPreviewViewController
            vc.videoURL = videoRecorded
        }
    }

}

extension TakePhotoViewController: AVCaptureFileOutputRecordingDelegate {

    func capture(_ captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAt fileURL: URL!, fromConnections connections: [Any]!) {


    }

    func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {
        if (error != nil) {
            print("Error recording movie: \(error!.localizedDescription)")
        } else {

            videoRecorded = outputURL! as URL
            print(videoRecorded)
            print("recorded")
            performSegue(withIdentifier: "showVideoPreview", sender: nil)


        }

    }

}

1 个答案:

答案 0 :(得分:0)

这段代码在这里工作,我和你有同样的问题。我必须在文件路径的末尾添加“mov”文件路径扩展名。见下文:

func recordVideo() {

    let recordingDelegate: AVCaptureFileOutputRecordingDelegate! = self

    let videoFileOutput = AVCaptureMovieFileOutput()
    videoOutput = videoFileOutput
    self.captureSession.addOutput(videoFileOutput)

    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let filePath = documentsURL.appendingPathComponent("introVideo").appendingPathExtension("mov")

    videoFileOutput.startRecording(toOutputFileURL: filePath, recordingDelegate: recordingDelegate)

}

然后我把那个url传递给我的VideoPlayerController,如下所示:

func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {

    if error == nil {

        let videoVC = PlayVideoController()
        videoVC.url = outputFileURL!
        self.navigationController?.pushViewController(videoVC, animated: true)
    }

    return
}

然后这是我在VideoPlayerController上的代码,用于播放刚录制的视频:

class PlayVideoController: UIViewController {

var url: URL!
var player: AVPlayer!
var avPlayerLayer: AVPlayerLayer!

override func viewWillAppear(_ animated: Bool) {

    if url != nil {

        print(url)
        player = AVPlayer(url: url)
        let playerLayer = AVPlayerLayer(player: player)
        self.view.layer.addSublayer(playerLayer)
        playerLayer.frame = self.view.frame
        player.play()

    }
}

}

希望这有帮助!