简体   繁体   English

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

[英]Videos stops and plays from the beginning instead of pausing ,swift AVPlayer

I have a uiView where I am playing a video with custom controls. 我有一个uiView,我正在其中使用自定义控件播放视频。

When I pause the video , it goes to the beginning of the video and pauses instead of pausing at that particular frame 当我暂停视频时,它会转到视频的开头并暂停,而不是在该特定帧暂停

Following is my Code 以下是我的代码

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)
        }

    }

I am setting up the videoPlayer in the ViewTableCell class as follows 我在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()
    }

The problem is on every on click of the play button "currentCell.videoPlayerItem = AVPlayerItem.init(url: path)" is being called which is replacing the videoPlayerItem. 问题是每次单击播放按钮时都会调用“ currentCell.videoPlayerItem = AVPlayerItem.init(url:path)”,它正在替换videoPlayerItem。 I want to pause the video at that particular frame and not go back to the beginning of the video. 我想在特定的帧处暂停视频,而不要回到视频的开头。

How can I overcome this problem. 我该如何克服这个问题。 Any help will be appreciated. 任何帮助将不胜感激。

You replace the videoPlayerItem every times you press the button. 每次按下按钮时,都将替换videoPlayerItem That's why it resets: 这就是它重置的原因:

avPlayer?.replaceCurrentItem(with: self.videoPlayerItem)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM