次は、カスタムコントロールを作成してビデオを再生しようとしているコードです。テーブルビューは、スクロールダウン中に他のセルのビデオを再生します
再生ボタンをクリックすると、テーブルビューのビデオが再生されますが、ビデオが再生されている現在のセルの3番目のセルで再生されます。
3番目のセルに移動してそこの再生ボタンをクリックすると、正しいビデオが再生されますが、同じビデオが2つ後に再生されます。
どうすればこの問題を解決できますか。どんな助けもありがとう。
class ViewTableCell: UITableViewCell {
var avPlayer: AVPlayer?
var avPlayerLayer: AVPlayerLayer?
var paused: Bool = false
var videoPlayerItem: AVPlayerItem? = nil {
didSet {
avPlayer?.replaceCurrentItem(with: self.videoPlayerItem)
}
}
var tapAction: ((ViewTableCell) -> Void)?
@IBAction func playButtonAction(_ sender: UIButton) {
tapAction?(self)
}
func setupMoviePlayer(view:UIView){
self.avPlayer = AVPlayer.init(playerItem: self.videoPlayerItem)
avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayerLayer?.videoGravity = AVLayerVideoGravityResizeAspect
avPlayer?.volume = 3
avPlayer?.actionAtItemEnd = .none
avPlayerLayer?.frame = view.bounds
self.backgroundColor = .clear
view.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()
}
override func awakeFromNib() {
super.awakeFromNib()
self.setupMoviePlayer(view: videoView)
// Initialization code
}
}
ビューコントローラクラス
var cell : TrendViewTableCell?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
//MARK CellForRowAt
let resusableIdentifier: String = "ViewControllerCell"
cell = (tableView.dequeueReusableCell(withIdentifier: resusableIdentifier) as? ViewTableCell)
if cell == nil {
cell = ViewTableCell(style:UITableViewCellStyle.default, reuseIdentifier: resusableIdentifier)
}
cell?.videoName.text = ArtistFeeds.sharedInstance.videoFeeds[indexPath.row].videoTitle
cell?.tapAction = { (cell) in
self.onTapPlayButton(cell)
}
cell?.playButton.addTarget(self, action: #selector(TrendViewController.onTapPlayButton), for: .touchUpInside)
return cell!
}
func onTapPlayButton(_ selectedCell:ViewTableCell){
if let index = self.trendTableView.indexPath(for: selectedCell){
//put your code to execute here
let url = Bundle.main.path(forResource:ArtistFeeds.sharedInstance.videoFeeds[index.row].videoUrl, ofType:"mp4")
let path = NSURL.fileURL(withPath: url!)
let currentCell = tableView.cellForRow(at: index) 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.setImage(playImage, for: .normal)
} else {
currentCell.startPlayback()
currentCell.playButton.setImage(pauseImage, for: .normal)
}
}
}
細胞は、再利用されます。セルクラス自体でタップを処理し、委譲パターンまたはクロージャを使用してtableviewコントローラに通知する必要があります。 http://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510 – Paulw11
私は、あなたが言及した上記の方法。しかし、同じ問題が残る。 @ Paulw11 –