gpt4 book ai didi

ios - 我无法在 swift 2.3 中播放保存在目录路径和 AVPlayer 中的视频

转载 作者:行者123 更新时间:2023-11-28 10:15:52 30 4
gpt4 key购买 nike

我正在用 AVFoundation 录制视频,并且我从视频创建了预览,当我完成录制视频时,视频正在预览中播放,但是当我使用以下方法保存视频时:

var fileName = "\(self.tField.text!).mp4"
fileName = fileName.stringByReplacingOccurrencesOfString(" ", withString: "_")

let path = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
//let originVideoFile = self.filePath
let destinationVideoFile = path.URLByAppendingPathComponent(fileName)
let data = NSData(contentsOfURL: self.filePath)

//try data!.writeToURL(destinationVideoFile!, options: [])
let fileManager = NSFileManager()
fileManager.createFileAtPath((destinationVideoFile?.path)!, contents: data, attributes: nil)

视频文件已创建,但当我想重新播放视频时无权访问此文件,但相机胶卷可以播放视频。

This is the preview of video

and this is the problem when I try to re play the video

为了得到“ filePath”,我使用了 AVFoundation的委托(delegate)方法。 :
func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {

print("capture did finish")
print(captureOutput);
print(outputFileURL);
self.filePath = outputFileURL
performSegueWithIdentifier("previewVideo", sender: self)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "previewVideo"{
let destination = segue.destinationViewController as! PreviewVC
destination.filePath = self.filePath
destination.videoDelegate = self.videoDelegate
}
}

并使用“ prepareForSegue”传递数据

我返回一个包含所需信息的视频对象,当我想重新播放视频时,我使用 Collection View 的方法“ didSelect”创建一个“ AVPlayer”并使用文件名找到视频的路径,则文件存在但无法播放
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {


videoSingleObject = videoObjects[indexPath.item]
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
let filePath = url.URLByAppendingPathComponent(videoSingleObject.nameOfFile)!.path!
let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(filePath) {
print("FILE AVAILABLE")
UISaveVideoAtPathToSavedPhotosAlbum(filePath, nil, nil, nil)
print("PATH: \(filePath)")
let videoPath = NSURL(string: filePath)!
//filePathURL = videoPath
print(videoPath)
asset = AVAsset(URL: videoPath)
playerItem = AVPlayerItem(asset: asset)
print("DURATION: \(CMTimeGetSeconds(asset.duration))")
player = AVPlayer(playerItem: playerItem)
playerViewController.player = player
self.presentViewController(playerViewController, animated: true, completion: nil)
player.play()
}else {
print("THE DIRECTORY NOT EXIST")
}
}

但是我使用“ UISaveVideoAtPathToSavedPhotosAlbum(filePath, nil, nil, nil)”来分享cameraRoll中的视频,并且在cameraRoll中视频是可以播放的。

最佳答案

我发现了错误,问题是 URL 引用,我使用的是 NSURL(string: filePath),我使用 NSURL(fileURLwithPath: filePath) 解决了问题

关于ios - 我无法在 swift 2.3 中播放保存在目录路径和 AVPlayer 中的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41531010/

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