Joc*_*rGG 0 xcode ios swift swift2
我正在使用 录制视频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)
Run Code Online (Sandbox Code Playgroud)
视频文件已创建,但当我想重新播放视频时,我无权访问此文件,但相机胶卷可以播放视频。
要获得“ filePath”,我正在使用以下委托方法AVFoundation:
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
}
}
Run Code Online (Sandbox Code Playgroud)
并用“ prepareForSegue”传递数据
我返回一个带有所需信息的视频对象,当我想重新播放视频时,我使用didSelectcollectionview的方法“ ”创建一个“ 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")
}
}
Run Code Online (Sandbox Code Playgroud)
但是我使用“ UISaveVideoAtPathToSavedPhotosAlbum(filePath, nil, nil, nil)”在cameraRoll中共享视频,并且在cameraRoll中可以播放视频。
我发现了错误,问题是 URL 引用,我使用的是 NSURL(string: filePath),我使用 NSURL(fileURLwithPath: filePath) 解决了问题
| 归档时间: |
|
| 查看次数: |
1430 次 |
| 最近记录: |