通过AVCaptureFileOutputRecordingDelegate函数给出的URL无法播放

时间:2018-05-22 16:07:39

标签: ios swift video avplayer playback

我正在尝试创建类似Snapchat的应用,用户可以通过按住按钮来拍摄视频。但是,当用户完成视频播放并且代理人调用fileOutput时,无法使用AVPlayer播放给定的网址outputFileURL。我知道视频实际上是录制的,因为我可以将文件上传到Firebase并从那里下载。

这是fileOutput函数的代码:

func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
    if error == nil {
        videoURL = outputFileURL
        flipHiddenViews()

        // playback video
        player = AVPlayer(url: outputFileURL)
        playerLayer = AVPlayerLayer(player: player)
        playerLayer?.frame = self.view.bounds
        self.view.layer.addSublayer(playerLayer!)
        player?.play()
    } else {
        print(error?.localizedDescription)
    }

}

以下是我初始化用户按下的按钮的方法:

let photoButton:UIButton = {
    let but = UIButton(type: .custom)
    but.layer.cornerRadius = 40
    but.layer.borderColor = UIColor.white.cgColor
    but.layer.borderWidth = 4
    but.clipsToBounds = true
    but.addTarget(self, action: #selector(takeVideo), for: .touchDown)
    but.addTarget(self, action: #selector(stopVideo), for: [.touchUpInside, .touchUpOutside])
    but.translatesAutoresizingMaskIntoConstraints = false
    return but
}()

这是takeVideo函数:

@objc func takeVideo() {
    let recordingDelegate:AVCaptureFileOutputRecordingDelegate? = self
    if captureSession?.outputs != nil && videoFileOutput != nil {
        captureSession?.removeOutput(videoFileOutput!)
    }
    videoFileOutput = AVCaptureMovieFileOutput()
    self.captureSession?.addOutput(videoFileOutput!)

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

    // Do recording and save the output to the `filePath`
    videoFileOutput?.startRecording(to: filePath, recordingDelegate: recordingDelegate!)
}

最后,stopVideo函数:

@objc func stopVideo() {
    videoFileOutput?.stopRecording()
}

我做错了什么?

2 个答案:

答案 0 :(得分:0)

请改为尝试:

// playback video
player = AVPlayer(url: outputFileURL)
let playerController = AVPlayerViewController()
playerController.player = player
present(playerController, animated: true) {
    player?.play()
}

答案 1 :(得分:0)

在" temp"的末尾添加var col = new THREE.Color('#ff00ff'); TweenLite.to(mesh.material.color, 1, { r: col.r, g: col.g, b: col.b, }); 名字为我做了:

.mov