Play Videos in TableViewCell
If you create a custom UITableViewCell, you can add a AVPlayer object to that cell and load it up with a video (some code you can use to start with can be found in this related question).
To be honest though, do not expect responsive or fast UI in your table view if you have 7 videos playing at the same time. This is going to be very taxing on the device (in terms of CPU and battery). And each time the "more" button is touched (to add more videos), you might get even slower performance (mitigated only by if you code things in a friendly way -- e.g. where the player stops when the cell is scrolled offscreen).
How to embed AVPlayerViewController in a TableViewCell
Update
Thanks for the help Alex. After reviewing that example, I was able to add video to to the cell once I returned a UIView which contained the AVPlayer.
let videoFrame: UIView = {
let frame = UIView(frame: CGRect(x: 0, y: 95, width: screenWidth, height: 211))
let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL!)
let video = AVPlayerViewController()
video.player = player
video.view.frame = frame.bounds
frame.addSubview(video.view)
video.player?.play()
return frame
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(videoFrame)
Embedded YouTube video in a UIWebView fails to play
Code
var testArray = ["https://www.youtube.com/embed/W7qWa52k-nE", "https://www.youtube.com/embed/vH7i-qpoFVk", "https://www.youtube.com/embed/V_GTrL-UVmE"]
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return testArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! WebViewTableViewCell
cell.videoWeb.allowsInlineMediaPlayback = true
let embededHTML = "<html><body><iframe src=\"\(testArray[indexPath.row])?playsinline=1\" width=\"'\(cell.videoWeb.frame.width)'\" height=\"'\(cell.videoWeb.frame.height)'\" frameborder=\"10\" allowfullscreen></iframe></body></html>"
cell.videoWeb.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().bundleURL)
cell.videoWeb.scrollView.scrollEnabled = false
return cell
}
Screenshot
Test Project
test project link
how to add Videos in UITableViewCell
1. This is one way of adding video in cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier =@"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSURL *videoURL = [NSURL URLWithString:videoURL];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[moviePlayer.view setFrame:CGRectMake(10.0, 0.0, 150.0 , 100.0)];
[cell.contentView addSubview:moviePlayer.view];
moviePlayer.view.hidden = NO;
[moviePlayer prepareToPlay];
[moviePlayer play];
return cell;
}
2. But the best way of adding video in cell is to subclass a UITableViewCell and add a MPMoviePlayerController as its Property
@implementation CustomVideoCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.movie = [[MPMoviePlayerController alloc] init];
self.movie.scalingMode = MPMovieScalingModeAspectFit;
[self.contentView addSubview:self.movie.view];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.movie.frame = CGRectMake(10, 0, self.bounds.size.width - 80, self.bounds.size.height);
}
In swift 4:
static let tableViewCellIdentifier = "CellID"
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: tableViewCellIdentifier)
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: tableViewCellIdentifier)
}
let videoURL = URL(string: videoURL ?? "")
if let videoURL = videoURL {
moviePlayer = MPMoviePlayerController(contentURL: videoURL)
}
moviePlayer.controlStyle = .none
moviePlayer.scalingMode = .aspectFit
moviePlayer.view.frame = CGRect(x: 10.0, y: 0.0, width: 150.0, height: 100.0)
cell?.contentView.addSubview(moviePlayer.view)
moviePlayer.view.hidden = false
moviePlayer.prepareToPlay()
moviePlayer.play()
return cell!
}
......
class CustomVideoCell {
init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
movie = MPMoviePlayerController()
movie.scalingMode = .aspectFit
contentView.addSubview(movie.view)
}
func layoutSubviews() {
super.layoutSubviews()
movie.frame = CGRect(x: 10, y: 0, width: bounds.size.width - 80, height: bounds.size.height)
}
}
Related Topics
How Does One Trap Arithmetic Overflow Errors in Swift
Check Whether Swift Object Is an Instance of a Given Metatype
Swift: Overriding == in Subclass Results Invocation of == in Superclass Only
How to "Strongify" Optional Self Using Guard in Swift 2.0
How to Connect to Self Signed Servers Using Alamofire 1.3
How to Check Is a String or Number
Understanding Swift 2.2 Selector Syntax - #Selector()
Swift Pointer Problems with MACh_Task_Basic_Info
How to Use Array.Filter to Filter a Class Object Based on a Property
Function Does Not Wait Until the Data Is Downloaded
Safariviewcontroller: How to Grab Oauth Token from Url
Previewprovider and Observedobject Properties