我曾经使用avplayer在UITableViewCell
上加载视频。我可以在UITableViewCell
上加载视频,但我无法暂停/播放视频。我不能做任何基本功能,如增加/减少音量。请任何人帮我解决这个问题。
这是我的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell: FeedCell = tableView.dequeueReusableCell(withIdentifier: "VedioCell", for: indexPath) as! FeedCell
let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
player = AVPlayer(url: videoURL! as URL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRect(x:0, y: 0, width: UIScreen.main.bounds.width, height: 200)
cell.PlayerView?.layer.addSublayer(playerLayer)
player.play()
return cell
}
我在这里使用了avplayer,但我无法访问
先谢谢。
答案 0 :(得分:1)
试试这个
func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - >的UITableViewCell {
let cell: FeedCell = tableView.dequeueReusableCell(withIdentifier: "VedioCell", for: indexPath) as! FeedCell
let player = playerArray[indexPath.row]
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRect(x:0, y: 0, width: UIScreen.main.bounds.width, height: 200)
cell.PlayerView?.layer.addSublayer(playerLayer)
cell.play.tag = indexPath.row
cell.play.addTarget(self, action: #selector(GetUpdatesButtonTouched), for: .touchUpInside)
return cell
}
@IBAction func GetUpdatesButtonTouched(_ sender: AnyObject) {
if sender.currentTitle == "Play" {
for i in 0..<10{
currentPlayer=playerArray[i]
currentPlayer.pause()
print("tag: \(sender.tag)")
print("tag: \(playerArray)")
currentPlayer=playerArray[sender.tag]
print("tag: \(currentPlayer)")
// handleVideoChageSlider(("" as AnyObject) as! UISlider)
currentPlayer.play()
sender.setTitle("Pause", for: UIControlState.normal)
// let indexPath = tableView.indexPathForSelectedRow!
let cell: FeedCell = tableView.cellForRow(at:sender.indexPath)as! FeedCell
//print(currentCell.videoLabel.text)
//cell.slider.tag=indexPath.row
cell.slider.addTarget(self, action: #selector(handleVideoChageSlider), for:.valueChanged )
}
}
else if sender.currentTitle == "Pause"{
for i in 0..<10{
currentPlayer=playerArray[i]
currentPlayer.pause()
sender.setTitle("Play", for: UIControlState.normal)
}
}
}
答案 1 :(得分:0)
尝试这种方法:
1)在AVPlayer
班级中声明cell
的变量。
2)在cellWillAppear
内,实例化此变量,如
cell.avplayer = AVPlayer(url: videoURL! as URL)
3)在cellWillDisapper
内,制作avplayer nil
cell.avplayer = nil
4)要增加/减少音量,请仅使用该特定avplayer
的{{1}}。
答案 2 :(得分:-1)
也许您应该重复使用AVPlayerLayer,或者“https”导致它