1

I want to play a video in each cell of a UITableView. I tried creating a mpmoviecontroller but there is no output - just a black square behind the uitableview. Any ideas?

Also, is this the best way of doing it?

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.frame         =   CGRectMake(0, 0, view.bounds.width, view.bounds.height);
    tableView.delegate      =   self
    tableView.dataSource    =   self

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

    self.view.addSubview(tableView)
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
    var moviePlayer : MPMoviePlayerController?

    let url = NSURL (string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")
    moviePlayer = MPMoviePlayerController(contentURL: url)
    if let player = moviePlayer {
        player.view.frame = CGRectMake(0, 100, view.bounds.size.width, 180)
        player.prepareToPlay()
        player.controlStyle = .None
        player.repeatMode = .One
        player.scalingMode = .AspectFit
        cell.addSubview(player.view)
    }

    return cell

}
Chris Jones
  • 856
  • 1
  • 11
  • 24
  • Use a custom cell instead – Hamza Ansari Aug 01 '15 at 07:45
  • Why do you want to play a video in a table cell? Design-wise I can think of very few situations were you would be better off doing that instead of simply putting the names in the tableview and segueing to a view controller where you play it. Even if it's a trailer (as I'm guessing it is), just create a nice simply UIView with the movie top and center, a nice big blue play button that segues further, and a back button to return to the tableview. – dunnmifflsys Aug 01 '15 at 09:08

1 Answers1

2

If you want a simple video without controls, try this answer:

https://www.johnxiong.com/2017/03/14/quick-swift-play-video-in-uitableviewcell/

dywp
  • 123
  • 2
  • 10
Abdulrahman Masoud
  • 1,096
  • 8
  • 12