gpt4 book ai didi

ios - AVCaptureFileOutputRecordingDelegate函数给出的URL无法播放

转载 作者:行者123 更新时间:2023-11-28 07:48:00 25 4
gpt4 key购买 nike

我正在尝试创建一个类似 Snapchat 的应用程序,用户可以在其中通过按住按钮来拍摄视频。但是,当用户完成拍摄视频并且委托(delegate)调用 fileOutput 时,无法使用 AVPlayer 播放给定的 url 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()
}

我做错了什么?

最佳答案

试试这个:

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

关于ios - AVCaptureFileOutputRecordingDelegate函数给出的URL无法播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50472122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com