private let VIDEO_EXTENSIONS = [
".MOV", ".MP4"
]
private var fileManager = NSFileManager.defaultManager()
func loadVideos() {
var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
if paths.count > 0 {
let documentsDirectory = paths[0] as String
let documentUrl = NSURL(fileURLWithPath: documentsDirectory, isDirectory: true)
do {
documentUrl.path
let files = try fileManager.contentsOfDirectoryAtPath(documentsDirectory)
for file in files {
fetchVideos(documentUrl.URLByAppendingPathComponent(file).path ?? "")
}
} catch {
}
self.tableView.reloadData()
}
}
func fetchVideos(path: String) {
var isDir: ObjCBool = false
if !path.isEmpty && fileManager.fileExistsAtPath(path, isDirectory: &isDir) {
if isDir {
do {
let files = try fileManager.contentsOfDirectoryAtPath(path)
for file in files {
fetchVideos(file)
}
} catch {
}
} else {
var file = File(path: path)
if file.isValid() && isVideoFileExtension(file.fileExtension.uppercaseString) {
do {
if let attr: NSDictionary = try fileManager.attributesOfItemAtPath(path) {
file.fileSize = attr.fileSize()
}
} catch {
}
videos.append(file)
}
}
}
}
func isVideoFileExtension(ext: String) -> Bool {
for videoExtension in VIDEO_EXTENSIONS {
if ext == videoExtension {
return true
}
}
return false
}
struct File {
var fileExtension = ""
var fileName = ""
var path = ""
var assert: AVURLAsset?
var url: NSURL!
var fileSize: UInt64 = 0
init(path: String) {
self.path = path
self.url = NSURL(fileURLWithPath: path)
self.fileName = url.lastPathComponent ?? ""
self.fileExtension = "." + (url.pathExtension ?? "")
}
func isValid() -> Bool {
return !(fileName.isEmpty || fileExtension.isEmpty)
}
}
func play(file: File) {
let player = AVPlayer(URL: file.url)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player?.play()
}
}
func openIn(file: File, indexPath: NSIndexPath) {
let document = UIDocumentInteractionController(URL: file.url)
let rect = self.tableView.rectForRowAtIndexPath(indexPath)
document.presentOpenInMenuFromRect(rect, inView: self.tableView, animated: true)
}
func delete(file: File, indexPath: NSIndexPath) {
do {
try fileManager.removeItemAtPath(file.path)
videos.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
} catch {
}
}
func saveToCameraRoll(file: File, indexPath: NSIndexPath) {
if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(file.path) {
UISaveVideoAtPathToSavedPhotosAlbum(file.path, self, "image:didFinishSavingWithError:contextInfo:", nil)
} else {
// save faild
}
}
func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafePointer<Void>) {
if error == nil {
// save success
} else {
// save faild
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有