2016-11-02 3 views
0

Я использовал пользовательский cell для воспроизведения видео в ячейке. Но все, что он делает, это показать мне пустой tableViewcell, может кто-нибудь помочь мне?Воспроизвести видео на Table 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 
} 
+0

Можете ли вы показать класс FeedCell? –

ответ

0

Убедитесь импортировать import AVKit и import 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 если вы показываете многие из них.

+0

Спасибо, м. Tarvo – Agaram

+0

, если я хочу воспроизвести видео, когда нажимаю кнопку внутри ячейки, как я могу это сделать – Agaram

+0

Работало? Просто используйте: '@IBAction FUNC newPostButtonAction (_ отправитель: AnyObject) { player.play() }' –