2017-01-08 2 views
0

gespeichert ist. Ich nehme ein Video mit AVFoundation auf, und ich habe eine Vorschau vom Video erstellt, wenn ich ein Video aufnehme, wird das Video in der Vorschau, aber wenn ich das Video speichern mit:Ich kann kein Video abspielen, das im Verzeichnispfad und AVPlayer in swift 2.3

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) 

die Videodatei erstellt wird, aber ich habe keinen Zugriff auf diese Datei, wenn ich will re das Video abspielen, aber die Kamera Rolle kann das Video abspielen.

This is the preview of video

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

bekommen die "filePath" Ich bin mit dem Delegatmethode von 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 
    } 
} 

und die Daten übergeben mit "prepareForSegue"

I gebe ein Videoobjekt mit den benötigten Informationen zurück, und wenn ich es erneut abspielen möchte Video, ich mit der Methode „didSelect“ der Kollektion ich ein „AVPlayer“ erstellen und verwenden den Namen der Datei, um den Pfad des Videos zu finden, dann ist die Datei existiert, aber nicht

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") 
    } 
} 

spielen können, aber Ich verwende "UISaveVideoAtPathToSavedPhotosAlbum(filePath, nil, nil, nil)", um das Video in der cameraRoll zu teilen, und in der cameraRoll kann das Video abgespielt werden.

+0

Großen Arbeits Code, ich das Problem mit NSURL (filePath fileURLwithPath) gelöst! –

Antwort

4

i den Fehler gefunden, das Problem ist die URL-Referenz, verwende ich eine NSURL (string: filePath):

Verwandte Themen