如何将视频保存到应用程序的目录并在视图控制器中播放?

时间:2017-02-07 22:46:52

标签: swift xcode

I'm trying to save a video in my app directory and then play it back in my view controller. I'm having an issue with saving and making the path. Can anyone help?

func imagePickerController(_ picker:UIImagePickerController,didFinishPickingMediaWithInfo info:[String:Any])     {

        // Save the video to the app directory
        let videoURL = info[UIImagePickerControllerMediaURL] as! NSURL
        let videoData = NSData(contentsOf: videoURL as URL)
        let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
        let documentsDirectory: AnyObject = paths[0] as AnyObject
        let dataPath = documentsDirectory.appendingPathComponent("/vid1.mp4")

        videoData?.write(toFile: dataPath, atomically: false)
        self.dismiss(animated: true, completion: nil)

}

@IBAction func playVideoAction(_ sender: Any)
{
    let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
    let documentsDirectory: AnyObject = paths[0] as AnyObject
    let dataPath = documentsDirectory.appendingPathComponent("/vid1.mp4")

    let videoAsset = (AVAsset(url: NSURL(fileURLWithPath: dataPath) as URL))
    let playerItem = AVPlayerItem(asset: videoAsset)

    let player = AVPlayer(playerItem: playerItem)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player

    self.present(playerViewController, animated: true)
    {
        playerViewController.player!.play()
    }
}

how to save video file into document directory

我已经使用了此链接中的代码,但是对xcode 8 / swift 3的更新没有那么有用。一段时间以来一直坚持这个问题。

1 个答案:

答案 0 :(得分:0)

您想使用PHImageManager.requestExportSession(forVideo:options:)。这将异步准备资产(包括在需要时下载),并创建可用于保存文件的AVExportSession。通过指定您想要原件,并通过内容(如果可能),您应该获得最佳质量的视频。

var dataPath: URL {
    let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
    let documentsDirectory = URL(fileURLWithPath: paths[0])
    return documentsDirectory.appendingPathComponent("/vid1.mp4")
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let url = info[UIImagePickerControllerReferenceURL] as! URL

    let assets = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil)
    let asset = assets.object(at: 0)
    let options = PHVideoRequestOptions()
    options.version = .original
    PHImageManager.default().requestExportSession(forVideo: asset, options: options, exportPreset: AVAssetExportPresetPassthrough) { (exportSession, info) in
        guard let session = exportSession else { return }
        session.outputURL = self.dataPath
        session.outputFileType = AVFileTypeMPEG4
        session.exportAsynchronously {
            DispatchQueue.main.async {
                self.dismiss(animated: true, completion: nil)
            }
        }
    }
}

如果您不想保存到磁盘而只是播放,可以使用requestPlayerItem获取可在播放器中使用的AVPlayerItem

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let url = info[UIImagePickerControllerReferenceURL] as! URL

    let assets = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil)
    let asset = assets.object(at: 0)
    let options = PHVideoRequestOptions()
    options.version = .original
    PHImageManager.default().requestPlayerItem(forVideo: asset, options: options) { (playerItem, info) in
        let player = AVPlayer(playerItem: playerItem)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player

        DispatchQueue.main.async {
            self.dismiss(animated: true, completion: nil)
            self.present(playerViewController, animated: true) {
                playerViewController.player!.play()
            }
        }
    }
}