假期來(lái)學(xué)習(xí)一下swift,今天學(xué)的是視頻背景,視頻背景就是QQ那個(gè)登錄時(shí)出現(xiàn)的那個(gè)小視頻,由于我并沒(méi)有找到那個(gè)視頻,所以自己拿了一個(gè)過(guò)來(lái)。這個(gè)是剛剛學(xué)習(xí)swift有很多地方不了解 希望大家給與幫助。?
gayHub:https://github.com/AkaShark/swift--
郵箱:1548742234@qq.com 好了接下來(lái)開(kāi)始正題了~
這個(gè)小小的例子分為三個(gè)部分首先是第一部分
第一部分:
對(duì)于視頻的處理
?open? func? compressVideoWithUrl(videoUrl url:URL, startTime:CGFloat, duration:CGFloat, completion: ((_videoPath:URL,_? error:NSError?)->Void)? ){
? ? DispatchQueue.global().async {
? ? ? ? //獲取多媒體的信息
? ? ? ? letasset =AVURLAsset(url:url)
? ? ? ? //AVAssetExportPresetHighestQuality? 壓縮的質(zhì)量 壓縮視頻
? ? ? ? letexportSession =AVAssetExportSession(asset: asset, presetName:"AVAssetExportPresetHighestQuality")
? ? ? ? //獲取沙盒路徑
? ? ? ? let paths: NSArray = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray
? ? ? ? //獲取路徑
? ? ? ? varoutputUrl = paths.object(at:0)as!String
? ? ? ? //定義文件管理
? ? ? ? letmanager =FileManager.default
? ? ? ? //獲取完整的視頻路徑
? ? ? ? outputUrl = outputUrl.convert.appending("/output.mp4")
? ? ? ? do
? ? ? ? {
? ? ? ? ? ? //將原文件刪除
? ? ? ? ? ? trymanager.removeItem(atPath: outputUrl)
? ? ? ? }
? ? ? ? catch_
? ? ? ? {
? ? ? ? }
? ? ? ? //如果原文件刪除了
? ? ? ? if!manager.fileExists(atPath: outputUrl)
? ? ? ? {
? ? ? ? ? ? ifletexportSession = exportSessionasAVAssetExportSession?{
? ? ? ? ? ? ? ? //將壓縮的視頻文件存到原目錄下
? ? ? ? ? ? ? ? exportSession.outputURL=URL(fileURLWithPath: outputUrl)
? ? ? ? ? ? ? ? //進(jìn)行優(yōu)化網(wǎng)絡(luò)使用 方便快速啟動(dòng) 快速啟動(dòng)
? ? ? ? ? ? ? ? exportSession.shouldOptimizeForNetworkUse=true
? ? ? ? ? ? ? ? //導(dǎo)出類(lèi)型
? ? ? ? ? ? ? ? exportSession.outputFileType=AVFileType.mp4
? ? ? ? ? ? ? ? //CMTimeMakeWithSeconds(a,b) a當(dāng)前時(shí)間,b每秒鐘多少幀
? ? ? ? ? ? ? ? //CMTimeMakeWithSeconds 專(zhuān)門(mén)是為視頻設(shè)計(jì)的時(shí)間
? ? ? ? ? ? ? ? letstart =CMTimeMakeWithSeconds(Float64(startTime),600)
? ? ? ? ? ? ? ? letduration =CMTimeMakeWithSeconds(Float64(duration),600)
? ? ? ? ? ? ? ? print(duration)
? ? ? ? ? ? ? ? letrange =CMTimeRangeMake(start,duration)
? ? ? ? ? ? ? ? exportSession.timeRange= range
//? ? ? ? ? ? ? ? 異步導(dǎo)出
? ? ? ? ? ? ? ? exportSession.exportAsynchronously
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? //導(dǎo)出的狀態(tài)
? ? ? ? ? ? ? ? ? ? switchexportSession.status{
? ? ? ? ? ? ? ? ? ? ? ? //完成
? ? ? ? ? ? ? ? ? ? caseAVAssetExportSessionStatus.completed:
? ? ? ? ? ? ? ? ? ? ? ? completion?(exportSession.outputURL!,nil)
? ? ? ? ? ? ? ? ? ? ? ? //失敗
? ? ? ? ? ? ? ? ? ? caseAVAssetExportSessionStatus.failed:
? ? ? ? ? ? ? ? ? ? ? ? print("Failed: \(exportSession.error)")
//? ? ? ? ? ? ? ? ? ? 取消
? ? ? ? ? ? ? ? ? ? case
? ? ? ? ? ? ? ? ? ? AVAssetExportSessionStatus.cancelled:
? ? ? ? ? ? ? ? ? ? ? ? print("Failed:\(exportSession.error)")
? ? ? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? ? ? print("default case")? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? }
}
自定義一個(gè)方法獲取視頻的本地地址和視頻的一些相關(guān)的信息,對(duì)視頻進(jìn)行壓縮處理使得視頻可以更好的播放
第二部分- 定義基類(lèi)VC
定義了AVPlayerViewController和他的一些默認(rèn)屬性(如播放的填充模式,是否循環(huán)播放等)作為基類(lèi)使用的時(shí)候只需要繼承和簡(jiǎn)單的配置就可以了
?fileprivatefuncsetMoviePlayerView(_url:URL){
?? ? ? letvideoCutter =VideoCutter()
? ? ? ? videoCutter.compressVideoWithUrl(videoUrl: url, startTime:startTime, duration:duration) { (path, error)in
? ? ? ? ? ? ifletvideoPath = pathasURL?{
? ? ? ? ? ? ? ? self.moviePlayer.player=AVPlayer(url: videoPath)
? ? ? ? ? ? ? ? self.moviePlayer.player?.play()
? ? ? ? ? ? ? ? //設(shè)置player的音量為 將self.moviePlayerSoundLevel轉(zhuǎn)為float類(lèi)型
? ? ? ? ? ? ? ? self.moviePlayer.player?.volume = Float(self.moviePlayerSoundLevel)
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? //生命周期
? ? overridefuncviewDidLoad() {
? ? ? ? super.viewDidLoad()
? ? ? ? moviePlayer.view.frame? =view.frame
? ? ? ? moviePlayer.showsPlaybackControls = false
? ? ? ? view.addSubview(moviePlayer.view)
? ? ? ? //將moviePlayer.view放在最后
? ? ? ? view.sendSubview(toBack:moviePlayer.view)
? ? ? ? // Do any additional setup after loading the view.
? ? }
第三部分-直接調(diào)用
調(diào)用很容易,直接寫(xiě)出簡(jiǎn)單的屬性設(shè)置,添加到view上就ok了
? ?//在沙盒中尋找視頻位置
? ? ? ? leturl =URL(fileURLWithPath:Bundle.main.path(forResource:"moments", ofType:"mp4")!)
? ? ? ? //設(shè)置
? ? ? ? videoFrame=view.frame
? ? ? ? fillMode = .resizeAspectFill
? ? ? ? alwaysRepeat = true
? ? ? ? sound=? true
? ? ? ? startTime=2.0
? ? ? ? alpha=0.8
? ? ? ? contentURL= url
? ? ? ? //交互關(guān)閉
? ? ? ? view.isUserInteractionEnabled = false
剛剛學(xué)習(xí)swift 感覺(jué)好挺好的 和oc 對(duì)比起來(lái)真的感覺(jué)簡(jiǎn)略了很多啊。
感覺(jué)自己的東西寫(xiě)的一直不怎么好也再?lài)L試著去寫(xiě)好。更加努力堅(jiān)持吧。