demos程序员iOS开发

iOS Swift 实现带有缓存的播放器

2016-04-21  本文已影响522人  云抱住阳光太阳没放弃发亮

用到的第三方库:
Alamofire 用于网络请求和视频下载
MobilePlayer 用于播放在线视频

创建Movie类:

import Foundation
import Alamofire

class Movie {
    
    private var url: NSURL //网络路径 如果本地路径存在则是本地路径
    
    init (url: NSURL) {
        
        self.url = url
    }

    //获取播放路径
    func getUrl() -> NSURL {
        
        if isExist() {
            
            let url = NSURL(fileURLWithPath: getFilePath())
            
            return url
        }
        
        return self.url
    }
    
    //通知下载 如果已经存在 不下载
    func postDownload() {
        
        if isExist() {
            
            return
        }
        
        let destination = Alamofire.Request.suggestedDownloadDestination()
 
        Alamofire.download(.GET, url, destination: destination).response { _, _, _, error in
            
            if let error = error {
                
                print("Failed with error: \(error)")
                
            } else {
                
                print("Downloaded file successfully")
            
            }
        }
        
    }
    
    //是否已经下载
    private func isExist() -> Bool{
        
        let fileManager = NSFileManager.defaultManager()
        
        let filePath = getFilePath()
        
        print(filePath)
        
        let exist = fileManager.fileExistsAtPath(filePath)
        
        return exist
    }
    
    //通过文件名称 获取文件路径
    private func getFilePath() -> String {
        
        let filePath = NSHomeDirectory() + "/Documents/" + getFileName()
        
        return filePath
    }
    
    //通过url 获取文件名称
    private func getFileName() -> String {
        
        let array = self.url.URLString.componentsSeparatedByString("/")
        
        return array.last!
    }
    
}

创建Movie对象:

let string = "视频路径"
        
let url = NSURL(string: string)!
        
let movie = Movie(url: url)
        
movie.postDownload() //下载

play(movie)//播放

播放方法:

import MobilePlayer

func play(movie: Movie) {
        
    let url = movie.getUrl()
        
    let playerVC = MobilePlayerViewController(contentURL: url)
        
    playerVC.activityItems = [url]
        
    presentMoviePlayerViewControllerAnimated(playerVC)
}


一边播放一遍下载,下载成功后会播放本地路径的视频。

清除所有缓存:

//清除所有缓存
func cleanAllCache() {
    
    let fileManager = NSFileManager.defaultManager()
        
    let myDirectory = NSHomeDirectory() + "/Documents"
        
    let fileArray:[AnyObject]? = fileManager.subpathsAtPath(myDirectory)
        
    for fn in fileArray!{
            
        try! fileManager.removeItemAtPath(myDirectory + "/\(fn)")
    }
        
}

这里是用的是Alamofire设置的下载路径,注意删除的路径要和下载的路径一致。

//获取所有缓存大小
    func getAllCacheSize() -> String {
        
        let fileManager = NSFileManager.defaultManager()
        
        let myDirectory = NSHomeDirectory() + "/Documents"
        
        let fileArray:[AnyObject]? = fileManager.subpathsAtPath(myDirectory)
        
        var allSize = 0.0
        
        for fn in fileArray!{
            
            let attr = try! fileManager.attributesOfItemAtPath(myDirectory + "/\(fn)")
            
            let size = (attr["NSFileSize"] as! Double) / 1000000 //单位为 M
            
            allSize += size
        }
        
        return String(format: "%.1f", allSize) //保留小数点后一位

    }

上一篇下一篇

猜你喜欢

热点阅读