如何控制 tableViewCell 中的 AVPlayer(通过滑块播放、暂停、进度和搜索)?

How to control AVPlayer in a tableViewCell (Play, Pause, Progress via slider and seek)?

我正在尝试使用 AVPlayerLayer 和 AVPlayer 使 tableViewCell 内的视图播放视频。它在点击按钮时播放和暂停。 这是成功完成的。 但是现在我想显示视频的进度。当我点击播放和暂停按钮时,我可以通过自定义委托在我的 tableView class 中获取 currentTime() 。但我需要继续观察进展。我没能为此找到任何代表(可能是我弄错了文档中列出的代表)。我如何实现我的目标?

这是我的 playerView class:

import UIKit
import AVKit
import AVFoundation

class CustomPlayerView: UIView {

    override static var layerClass: AnyClass {
        return AVPlayerLayer.self
    }

    var playerLayer: AVPlayerLayer {
        return layer as! AVPlayerLayer
    }

    var player: AVPlayer? {
        get {
            return playerLayer.player
        }
        set {
            playerLayer.player = newValue
        }
    }
}

这是 tableViewCell class:

import UIKit
import AVFoundation
import AVKit

class VideoCell: UITableViewCell {

    @IBOutlet weak var myPlayerView: CustomPlayerView!
    @IBOutlet var playButton: UIButton!
    @IBOutlet var mySlider: UISlider!

    var videoPlayDelegate: videoDidplay!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

}

这是 cellForRow 和自定义委托函数:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "vidCell", for: indexPath) as! VideoCell
        let vidUrl = aRR_VidURL[indexPath.row]
        let avPlayer = AVPlayer(url: URL(string: vidUrl)!)
        cell.myPlayerView.playerLayer.player = avPlayer
        cell.playButton.tag = indexPath.row
        cell.playButton.addTarget(self, action: #selector(playAction(sender:)), for: .touchUpInside)
        return cell
    }

@objc fileprivate func playAction(sender: UIButton) {
        let cell = tableView.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as! VideoCell
        cell.videoPlayDelegate = self
        if sender.currentTitle == "Play" {
            cell.myPlayerView.player?.play()
            sender.setTitle("Pause", for: .normal)
            cell.videoPlayDelegate.videoPlaying(cell: cell, playerView: cell.myPlayerView)
        } else if sender.currentTitle == "Pause" {
            cell.myPlayerView.player?.pause()
            sender.setTitle("Play", for: .normal)
            cell.videoPlayDelegate.videoPlaying(cell: cell, playerView: cell.myPlayerView)
        }
    }

protocol videoDidplay {
    func videoPlaying(cell: VideoCell, playerView: CustomPlayerView)
}

谢谢!

您必须像为按钮操作编写函数一样为滑块编写函数。 可能是这样的:

cellForRow方法更改为:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "vidCell", for: indexPath) as! VideoCell
        let vidUrl = aRR_VidURL[indexPath.row]
        let avPlayer = AVPlayer(url: URL(string: vidUrl)!)
        cell.myPlayerView.playerLayer.player = avPlayer  
        cell.slider.addTarget(self, action: #selector(playbackSliderValueChanged), for: .valueChanged)
        cell.playButton.tag = indexPath.row
        cell.playButton.addTarget(self, action: #selector(playAction(sender:)), for: .touchUpInside)
        return cell
    }

滑块函数:

func playbackSliderValueChanged(sender: UISlider)
{
    let position = (sender as AnyObject).convert(CGPoint.zero, to: self.tableView)
    let indexPath = self.tableView.indexPathForRow(at: position)!
    let cell = tableView.cellForRow(at: IndexPath(row: indexPath.row, section: 0)) as! VideoCell

    if let duration = cell.myPlayerView.player?.currentItem?.duration
    {
        let totalSeconds = CMTimeGetSeconds(duration)
        let value = Float64(sender.value) * totalSeconds
        let seekTime = CMTime(value: Int64(value), timescale:1)
        cell.myPlayerView.player?.seek(to: seekTime)
    }

    if cell.myPlayerView.player!.rate == 0
    {
        cell.myPlayerView.player?.play()
    }
    else
    {
        cell.myPlayerView.player?.pause()

    }
}