gpt4 book ai didi

swift - 如何回放录制的视频?

转载 作者:行者123 更新时间:2023-11-30 12:17:52 25 4
gpt4 key购买 nike

这是我尝试播放的视频文件类型的示例:文件:///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)


}

}

}

最佳答案

这段代码在这里工作,我遇到了和你一样的问题。我必须在文件路径末尾添加“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()

}
}

}

希望这有帮助!

关于swift - 如何回放录制的视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45176993/

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