在Table Cell上播放视频

时间:2016-11-02 10:37:16

标签: ios swift uitableview

我使用自定义cell在单元格中播放视频。但它所做的只是向我显示空tableView cell,有人可以帮助我吗?

override func viewDidLoad() {
    super.viewDidLoad()

    let Custome = CustomeNavigation(nibName: "CustomeNavigation", bundle: nil)

    self.view.addSubview(Custome.view)
    Custome.lbl_customeTitle.text = "FEED"

    tableView.frame         =   CGRect(x:0, y:0, width:view.bounds.width, height:view.bounds.height);
    tableView.delegate      =   self
    tableView.dataSource    =   self

    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

    self.view.addSubview(tableView)

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return 2
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
        {
     let cell = tableView.dequeueReusableCell(withIdentifier: "VedioCell", for: indexPath) as! FeedCell

     let videoURL = NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
     let player = AVPlayer(url: videoURL! as URL)

     let playerLayer = AVPlayerLayer(player: player)
     playerLayer.frame = cell.bounds

     cell.layer.addSublayer(playerLayer)
     player.play()

     return cell
 }

1 个答案:

答案 0 :(得分:0)

确保导入import AVKitimport AVFoundation

你可以在cellForRowAtIndexPath内尝试这样做:

let cell: FeedCell = tableView.dequeueReusableCellWithIdentifier("VedioCell", forIndexPath: indexPath) as! FeedCell

let videoURL = NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(URL: videoURL!)

let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = cell.bounds

cell.layer.addSublayer(playerLayer)
cell?.playerView?.layer.addSublayer(playerLayer)
player.play()

return cell

如果您使用的是swift3,则语法如下:

let videoURL = NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL! as URL)

let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = (cell?.bounds)!

cell?.layer.addSublayer(playerLayer)
cell?.playerView?.layer.addSublayer(playerLayer)
player.play()

如果你展示了很多这些内容,你还必须确保你处理cellForRowAtIndexPath