美文网首页
Swift 视频切换界面不间断播放,断点续播。

Swift 视频切换界面不间断播放,断点续播。

作者: 过载_4478 | 来源:发表于2019-11-27 21:55 被阅读0次

    需要你在了解AVPlayer播放器的基础上来学习。
    基本上核心都是有注释的地方,如果你对AVPlayer播放器很熟悉只看注释的地方就可以。

    在CollectionView创建播放器,点击后跳转到新界面继上次播放时间继续播放

    import UIKit
    import AVKit
    let SCREEN_WIDTH = UIScreen.main.bounds.size.width
    let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
    class ViewController: UIViewController {
        private var collectionView:UICollectionView?
        var player : AVPlayer!
        var playerItem : AVPlayerItem!
        var urlAsset: AVURLAsset!
        override func viewDidLoad() {
            super.viewDidLoad()
           setUI()
        }
        func setUI() {
    
            let layout = UICollectionViewFlowLayout.init()
    
            layout.itemSize = CGSize(width: SCREEN_WIDTH, height: 300)
            let rect = CGRect(x: 0, y: 84, width:SCREEN_WIDTH , height: SCREEN_HEIGHT-64)
            collectionView = UICollectionView(frame: rect, collectionViewLayout: layout)
            collectionView?.delegate = self
            collectionView?.dataSource = self
            view.addSubview(collectionView!)
            collectionView?.register(CollectionViewCell.self, forCellWithReuseIdentifier: "identifier")
            collectionView?.backgroundColor = UIColor.lightGray
    
    
        }
    
    }
    
    extension ViewController:
        UICollectionViewDelegate,
        UICollectionViewDataSource{
    
    
        func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 1
        }
    
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 1
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifier", for: indexPath) as! CollectionViewCell
    
            return cell
        }
        func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
            
    
            playCellVideo(cell: cell as! CollectionViewCell)
    
        }
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            self.player.pause()
            let video = VideoViewController()
    //当前播放时间
            video.time = self.playerItem.currentTime()
    //视频帧率,没有这个值续播时间不准确,可能提前几秒,可延后几秒
            video.timeScale = Int32(self.urlAsset.duration.timescale)
    //视频链接
            video.urlAsset = self.urlAsset
    //我们需要把这3个值传过去就可以做到不间断播放
            self.navigationController?.pushViewController(video, animated: true)
    
        }
    
        func playCellVideo(cell: CollectionViewCell){
    
    
            self.urlAsset = (AVAsset.init(url: NSURL(string: "https://vt1.doubanio.com/201609291737/4af83e686c0432c0dbd3c320f14eba6f/view/movie/M/302030039.mp4")! as URL) as! AVURLAsset)// swiftlint:disable:this force_cast
    
            self.playerItem = AVPlayerItem.init(asset: self.urlAsset)
            self.player = AVPlayer.init(playerItem: self.playerItem)
            self.player.rate = 1.0
    
            let playerLayer = AVPlayerLayer.init(player: self.player)
    
            playerLayer.frame = cell.contentView.bounds
            playerLayer.videoGravity = AVLayerVideoGravity.resizeAspect
            playerLayer.contentsScale = UIScreen.main.scale
            playerLayer.backgroundColor = UIColor.white.cgColor
            cell.contentView.layer.addSublayer(playerLayer)
            self.player.play()
    
    
        }
    
        
    }
    

    新界面里继续播放

    import UIKit
    import AVKit
    class VideoViewController: UIViewController {
        var urlAsset: AVURLAsset!
        var timeScale: CMTimeScale!
        var player : AVPlayer!
        var playerItem : AVPlayerItem!
        var time : CMTime!
        override func viewDidLoad() {
            super.viewDidLoad()
            setVideoUI()
    
        }
        
        func setVideoUI(){
            self.playerItem = AVPlayerItem.init(asset: self.urlAsset)
            self.player = AVPlayer.init(playerItem: self.playerItem)
            self.player.rate = 1.0
    
            let playerLayer = AVPlayerLayer.init(player: self.player)
    
            playerLayer.frame = CGRect(x: 0, y: 84, width: self.view.frame.width, height: 500)
            playerLayer.videoGravity = AVLayerVideoGravity.resizeAspect
            playerLayer.contentsScale = UIScreen.main.scale
            self.view.layer.addSublayer(playerLayer)
    // 通过传过来帧率和时间换算出来当前播放器播放的时间
            let time = CMTimeMakeWithSeconds(CMTimeGetSeconds(self.time), preferredTimescale: self.timeScale);
    //对播放进行时间定位, toleranceBefore:提前多长时间 toleranceAfter:延后多长时间,因为我们做了时间换算所有并不需要提前和延后。(实现了精准定位)
            self.player.seek(to:time, toleranceBefore: CMTime.zero, toleranceAfter: CMTime.zero)
            self.player.play()
    
    }
    
    
    }
    

    这只是一个简单的例子,有什么问题可以给我留言

    相关文章

      网友评论

          本文标题:Swift 视频切换界面不间断播放,断点续播。

          本文链接:https://www.haomeiwen.com/subject/jhymwctx.html