swift 3 Avplayer图层在第二次播放相同视频时不可见

时间:2017-02-01 11:58:23

标签: ios swift3 avplayer

在我的应用程序中,我使用了 public static String extract(String myStr) { Pattern p = compile("Name:(?m)^.*$"); Matcher m = p.matcher(myStr); while (m.find()) { String theGroup = m.group(0); System.out.format("'%s'\n", theGroup); return m.group(0); } return null; } 来播放视频,如果我们播放视频并向前一页播放视频,则可以在后面播放视频,但如果我们再播放相同的视频,我们必须继续显示视频,它不应该从开始,这里我可以根据视频持续时间设置滑块位置,但不显示视频内容声音的屏幕即将到来。

第一次播放任何视频我称之为方法

AVPlayer

如果用户第二次播放相同的视频,我会检查存储在userdefaults中的当前网址,如果它正在播放相同的视频,我确实喜欢关注

  func setup()  {
        UserDefaults.standard.set(videoString as NSString, forKey: CurrentURL)
        UserDefaults.standard.synchronize()
        let targetTime:CMTime = CMTimeMake(0, 1)
        mediaPlayer.seek(to: targetTime)
        mediaPlayer.pause()

        print("Video URL \(videoString)")
        let url = NSURL(string: videoString)
        let playerItem = AVPlayerItem(url: url! as URL)
        mediaPlayer=AVPlayer(playerItem: playerItem)
        let playerLayer=AVPlayerLayer(player: mediaPlayer)
        playerLayer.frame =  self.view.bounds
        playerLayer.backgroundColor = UIColor.black.cgColor
        //playerLayer.videoGravity = AVLayerVideoGravityResize
        playerLayer.videoGravity = AVLayerVideoGravityResizeAspect
        self.view.layer.addSublayer(playerLayer)


        AppDelegate.shared().showLoading()
        videoView.isHidden = false
        mediaPlayer.play()
        let duration : CMTime = mediaPlayer.currentItem!.asset.duration
        let seconds : Float64 = CMTimeGetSeconds(duration)
        videoSlider.maximumValue = Float(seconds)
        videoPlayButton.setImage(UIImage(named: "Pause"), for: .normal)
        videoPlaying = true
        //Adding Default Periodic Observer On Player
        mediaPlayer.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1), queue: DispatchQueue.main) { (CMTime) -> Void in
            if mediaPlayer.currentItem?.status == .readyToPlay {
                AppDelegate.shared().removeLoading()
                self.videoSlider.value = Float(CMTimeGetSeconds(mediaPlayer.currentTime()))
                let currentTime : Int = Int(CMTimeGetSeconds(mediaPlayer.currentTime()))
                let minutes = currentTime/60
                let seconds = currentTime - minutes * 60
                self.durationLabel.text = NSString(format: "%02d:%02d", minutes,seconds) as String

            }
        }
    }

但是,如果我再次播放相同的视频,则lauer不会显示视频内容,如果用户播放已经运行的视频,则应该从当前持续时间继续而不是重新开始

0 个答案:

没有答案