视频从一开始就停止播放,而不是暂停,快速AVPlayer

时间:2017-04-13 07:21:24

标签: ios swift avplayer avplayeritem avplayerlayer

我有一个uiView,我正在播放带有自定义控件的视频。

当我暂停视频时,它会转到视频的开头并暂停而不是暂停在该特定的帧上

以下是我的代码

func playButtonTapped(cell: ViewTableCell) {
        guard let indexPath = self.tableView.indexPath(for: cell) else {
            return
        }
        let url = Bundle.main.path(forResource:ArtistFeeds.sharedInstance.videoFeeds[indexPath.row].videoUrl, ofType:"mp4")

        let path = NSURL.fileURL(withPath: url!)
        currentCell = tableView.cellForRow(at: indexPath) as! ViewTableCell
        currentCell.videoPlayerItem = AVPlayerItem.init(url: path)

        let playImage = UIImage(named: "image_video_play") as UIImage?
        let pauseImage = UIImage(named: "image_video_pause") as UIImage?
        if currentCell.avPlayer?.rate == 1.0 {
            currentCell.stopPlayback()
            currentCell.playButton.isHidden = false
            currentCell.playButton.setImage(playImage, for: .normal)

        } else {
            currentCell.startPlayback()
            currentCell.playButton.isHidden = true
            currentCell.playButton.setImage(pauseImage, for: .normal)
        }

    }

我在ViewTableCell类中设置videoPlayer,如下所示

var avPlayer: AVPlayer?
    var avPlayerLayer: AVPlayerLayer?
    var videoPlayerItem: AVPlayerItem? = nil {
        didSet {

            avPlayer?.replaceCurrentItem(with: self.videoPlayerItem)
        }
    }
    @IBAction func playButtonAction(_ sender: UIButton) {
        self.delegate?.playButtonTapped(cell: self)
    }


    func setupMoviePlayer(){
        self.avPlayer = AVPlayer.init(playerItem: self.videoPlayerItem)
        avPlayerLayer = AVPlayerLayer(player: avPlayer)
        avPlayerLayer?.videoGravity = AVLayerVideoGravityResizeAspect
        avPlayer?.volume = 3
        avPlayer?.actionAtItemEnd = .none
        avPlayerLayer?.frame = videoView.bounds
        self.backgroundColor = .clear
        videoView.layer.insertSublayer(avPlayerLayer!, at: 0)

        let interval = CMTime(value: 1, timescale: 2)
        avPlayer?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using :{ (progressTime) in
            let seconds = CMTimeGetSeconds(progressTime)

            if let duration = self.avPlayer?.currentItem?.duration{
                let durationSeconds = CMTimeGetSeconds(duration)
                self.videoSlider.value = Float(seconds/durationSeconds)
            }
        })

    }

    func stopPlayback(){
        self.avPlayer?.pause()
    }
    func startPlayback(){
        self.avPlayer?.play()
    }

问题在于每次点击播放按钮“currentCell.videoPlayerItem = AVPlayerItem.init(url:path)”正在调用,它正在替换videoPlayerItem。 我想在该特定帧暂停视频,而不是回到视频的开头。

我怎样才能克服这个问题。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

每次按下按钮都会替换Option Explicit Sub Format_Change() Dim sht As Worksheet For Each sht In Worksheets With sht .Range("M2").FormulaR1C1 = "=RC[-12]*1" .Range("M2").AutoFill Destination:=.Range("M2:W2"), Type:=xlFillDefault .Range("M2:W2").AutoFill Destination:=.Range("M2:W3000"), Type:=xlFillDefault .Range("M2:W3000").Copy .Range("A2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Application.CutCopyMode = False .Range(.Range("M2"), .Range("M2").CurrentRegion).ClearContents End With Next sht End Sub 。这就是它重置的原因:

videoPlayerItem