【问题标题】:How to control AVPlayer in a tableViewCell (Play, Pause, Progress via slider and seek)?如何在 tableViewCell 中控制 AVPlayer(通过滑块和搜索播放、暂停、进度)?
【发布时间】:2018-07-26 07:34:41
【问题描述】:

我正在尝试使用 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)
}

谢谢!

【问题讨论】:

    标签: ios swift swift4 avplayer


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢!它有帮助!
    • 在这种情况下我们是否可以使用 AVPlayer 的默认控件?我有与此相同的类,但我不想添加按钮和滑块我想为播放器使用默认播放器控制器我该怎么做?
    猜你喜欢
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    相关资源
    最近更新 更多