嘗試此斯威夫特3
var uniqueVideoID = ""
var videoURL:NSURL? = NSURL()
var uniqueID = ""
//Add this to ViewDidLoad
uniqueID = NSUUID().UUIDString
//Getting the path as URL and storing the data in myVideoVarData.
videoURL = info[UIImagePickerControllerMediaURL] as? URL as NSURL?
let myVideoVarData = try! Data(contentsOf: videoURL! as URL)
//Now writeing the data to the temp diroctory.
let tempPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let tempDocumentsDirectory: AnyObject = tempPath[0] as AnyObject
uniqueVideoID = uniqueID + "TEMPVIDEO.MOV"
let tempDataPath = tempDocumentsDirectory.appendingPathComponent(uniqueVideoID) as String
try? myVideoVarData.write(to: URL(fileURLWithPath: tempDataPath), options: [])
//Getting the time value of the movie.
let fileURL = URL(fileURLWithPath: tempDataPath)
let asset = AVAsset(url: fileURL)
let duration : CMTime = asset.duration
videoAlertIdVar = duration.value // Control for the runVideoAlert function.
//Now we remove the data from the temp Document Diroctory.
do{
let fileManager = FileManager.default
try fileManager.removeItem(atPath: tempDataPath)
} catch {
//Do nothing
}
// Cheacking to see if video is under the 18500 (:30 seconds).
if duration.value <= 18500 {
yesOrNo = "no" //Control for the button in the mainViewController is hidden or not. The default is "yes"
//Here we are writing the data to the Document Directory for use later on.
let docPaths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentsDirectory: AnyObject = docPaths[0] as AnyObject
uniqueVideoID = uniqueID + "VIDEO.MOV"
let docDataPath = documentsDirectory.appendingPathComponent(uniqueVideoID) as String
try? myVideoVarData.write(to: URL(fileURLWithPath: docDataPath), options: [])
print("docDataPath under picker ",docDataPath)
//This creates a thumbnail image.
let assetImageGenerate = AVAssetImageGenerator(asset: asset)
assetImageGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMake(asset.duration.value/3, asset.duration.timescale)
//This adds the thumbnail to the imageview.
if let videoImage = try? assetImageGenerate.copyCGImage(at: time, actualTime: nil) {
videoThumbnailOutlet.image = UIImage(cgImage: videoImage)
}
}else{
//Do nothing
}
播放視頻
//Note: The _videoData_ is the same as the _uniqueID_
var videoData = ""
func play(){
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let videoDataPath = documentsDirectory + "/" + videoData + "VIDEO.MOV"
let filePathURL = URL(fileURLWithPath: videoDataPath)
let player = AVPlayer(url: filePathURL)
let playerController = AVPlayerViewController()
playerController.player = player
self.present(playerController, animated: true) {
player.play()
}
}
不要整部電影數據加載到內存中,你的應用程序可能會崩潰deppending文件大小。只需使用文件管理器移動項目在url方法將其移動到新位置 –
http://stackoverflow.com/a/33189238/2303865 –