ios - 在表格单元格上播放视频

标签 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
 }

最佳答案

确保导入导入 AVKit导入 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

关于ios - 在表格单元格上播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40377822/

相关文章:

ios - 在应用程序启动时启动视频失败 iOS

iphone - 以编程方式编辑 iOS 联系人

ios - 如何在 "touching"嵌入 iframe 时禁用滚动?

iphone - iOS 应用程序所需的图标

ios - 在静态 UITableView 上插入行

ios - 在 SWIFT 中推送之前如何加载 View ?

ios - 从互联网下载图像后将图像设置为 UIlabel

ios - Swift - 自定义 View 以显示空表格单元格

ios - Swift iOS 意图扩展 : The data couldn't be read because it isn't in the correct format

ios - 在 Swift 中删除分组表中的行时崩溃