AVPlayer加载gif,直到加载视频的第一帧

时间:2019-04-05 04:03:41

标签: ios swift avplayer

我正在使用AVPlayer播放视频。我必须加载gif,直到视频的第一帧加载完毕。我同样使用SwiftyGif

我已经阅读了很多堆栈文章。我已尝试使用视频播放器的不同方法,例如.readyToPlay.unknown,但无法访问第一帧加载。有时它运行良好,但有时甚至在视频播放时,负载也不会到达或停留在那里

func showLoadingGif() {
        let gifManager = SwiftyGifManager(memoryLimit:50)
        if UIDevice.current.userInterfaceIdiom == .pad {
            let gif = UIImage(gifName: "loading.gif")
            imgViewSplash = UIImageView(gifImage: gif, manager: gifManager)
        }
        else
        {
            let gif = UIImage(gifName: "loading_mobile.gif")
            imgViewSplash = UIImageView(gifImage: gif, manager: gifManager)
        }
        imgViewSplash.loopCount = 10000
        // imageview.delegate = self
        imgViewSplash.frame = CGRect(x: 0.0, y: 0.0, width: self.view.bounds.size.width, height: self.view.bounds.size.height+100)
        imgViewSplash.center = CGPoint(x: view.frame.size.width  / 2, y: view.frame.size.height / 2)
        if self.view.subviews.contains(imgViewSplash) {
            self.imgViewSplash.removeFromSuperview() // Remove it
        }
        self.view.addSubview(imgViewSplash)

    }

    func hideLoadingGif() {
        if self.view.subviews.contains(imgViewSplash) {
            self.imgViewSplash.removeFromSuperview() // Remove it
        }
    }



self.observer = playerVideo.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1), queue: DispatchQueue.main) { (CMTime) -> Void in
            if self.playerVideo.currentItem?.status == .readyToPlay {
                self.updateSlider(elapsedTime: CMTime)

                if let currentItem = self.playerVideo.currentItem {
                    let duration = currentItem.asset.duration
                    print("Total Duration - \(duration)")
                    self.hideLoadingGif()
                }

                let time : Float64 = CMTimeGetSeconds(self.playerVideo.currentTime());
                //print("\(time)")
                print("App Into Overlay - \(time)")

                let playbackLikelyToKeepUp = self.playerVideo.currentItem?.isPlaybackLikelyToKeepUp
                if playbackLikelyToKeepUp == false {
                    //print("Here start the activity indicator inorder to show buffering")
                }else{
                    //print("stop the activity indicator")
                }
            }
            if self.playerVideo.currentItem?.status == .unknown
            {
                self.btnPlay.isUserInteractionEnabled = true
                self.progressMainView.isUserInteractionEnabled = true
                self.hideLoadingGif()
            }
        }

预期的结果应该是在加载视频时加载gif,并且在加载第一帧操作系统时应隐藏gif,并且应该播放视频,但实际上gif来了,有时在第一个帧加载之前隐藏,并且有时隐藏呆在那里,视频开始播放。

0 个答案:

没有答案