Search code examples
iosswiftswift4avplayer

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


I am trying to make a view inside a tableViewCell play a video using AVPlayerLayer and AVPlayer. It plays and pauses on button tap. This is done successfully. But now I want to show the progress of the video. I can get the currentTime() in my tableView class through a custom delegate when I tap play and pause button. But I need to keep observing the progress. I failed to find any delegate for that (may be I have mistaken the delegates listed in documentation). How do I achieve my goal?

Here's my 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
        }
    }
}

Here is the 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()
    }

}

Here are the cellForRow, and custom delegate functions:

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

Thanks!


Solution

  • You have to write a function for slider as you have written for the button action. May be like this:

    The cellForRow method changes to:

    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
        }
    

    Slider function:

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