我正在尝试使用AVPlayerLayer和AVPlayer在tableViewCell内播放视频。轻按按钮即可播放和暂停。
这已成功完成。
但是现在我想展示视频的进度。当我点击播放和暂停按钮时,可以通过自定义委托在tableView类中获得currentTime()
。但是我需要继续观察进度。我没有找到任何代表(可能是我误认为了文档中列出的代表)。我如何实现我的目标?
这是我的playerView类:
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类:
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)
}
谢谢!
答案 0 :(得分:0)
您必须为按钮动作编写滑块的功能。 可能是这样的:
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()
}
}