代码改变世界

swift开发笔记15

2017-09-07 09:09  dengchaojie_learner  阅读(185)  评论(0编辑  收藏  举报

swift开发笔记15

// swift中的String和OC中的NSString转换

extension String {

  var convert: NSString { return (self as NSString) }

}

 

    var backgroundColor: UIColor = UIColor.black {

        didSet {

            view.backgroundColor = backgroundColor

        }

    }

 

  open var duration: CGFloat = 0.0// 不同于Float

 

// 监听通知

                NotificationCenter.default.addObserver(self, selector: #selector(VideoSpashViewController.playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: moviePlayer.player?.currentItem)

 

// 在主线程刷新UI,播放视频

if let path = newUrl as URL? {

                DispatchQueue.global().async {

                    DispatchQueue.main.async {

                        self.moviePlayer.player = AVPlayer.init(url: path)

                        self.moviePlayer.player?.play()

                        self.moviePlayer.player?.volume = Float(self.moviePlayerSoundLevel)

                    }

                }

            }

 

        view.addSubview(moviePlayer.view)

        view.sendSubview(toBack: moviePlayer.view)// 将视频播放器的view放到最前面

 

// 视频转码

import AVFoundation

class VideoCutter: NSObject {

    open func cropVideoWithUrl(videoUrl url: URL, startTime: CGFloat, duration: CGFloat, completion: ((_ videoPath: URL?, _ error: NSError?) -> Void)?) {

        DispatchQueue.global().async {

            let asset = AVURLAsset.init(url: url)

            let exportSession = AVAssetExportSession.init(asset: asset, presetName: "AVAssetExportPresetHighestQuality")

            let paths :NSArray = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true) as NSArray

            var outputURL = paths.object(at: 0) as! String

            let manager = FileManager.default

            do {

                try manager.createDirectory(atPath: outputURL, withIntermediateDirectories: true, attributes: nil)

            } catch {

                

            }

            outputURL = outputURL.convert.appendingPathComponent("output.mp4")

            do {

                try manager.removeItem(atPath: outputURL)

            } catch {}

            if let exportSession = exportSession as AVAssetExportSession? {

                exportSession.outputURL = URL.init(fileURLWithPath: outputURL)

                exportSession.shouldOptimizeForNetworkUse = true

                exportSession.outputFileType = AVFileTypeMPEG4

                let start = CMTimeMakeWithSeconds(Float64(startTime), 600)

                let duration = CMTimeMakeWithSeconds(Float64(duration), 600)

                let range = CMTimeRangeMake(start, duration)

                exportSession.timeRange = range

                exportSession.exportAsynchronously { () -> Void in

                    switch exportSession.status {

                    case AVAssetExportSessionStatus.completed:

                        completion?(exportSession.outputURL, nil)

                    case AVAssetExportSessionStatus.failed:

                        print("Failed: \(String(describing: exportSession.error))")

                    case AVAssetExportSessionStatus.cancelled:

                        print("Failed: \(String(describing: exportSession.error))")

                    default:

                        print("default case")

                    }

                }

                

            }

            DispatchQueue.main.async {

                

            }

        }

        

    }

}