1

How can I only play video for visible cells? Stop the video once the cell is not visible ?

I know it was something to do with indexPathsForVisibleRows but having issue figuring how to implement it

Once Cell is visible at a time currently.

.......

import UIKit
import AVKit
import AVFoundation

class videoDevTable: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 3
}

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

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! VideoTableViewCell

    let videoURL = NSURL(string: "https://scontent.cdninstagram.com/t50.2886-16/12951809_1538642766431783_2068695559_n.mp4")
    let player = AVPlayer(URL: videoURL!)

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

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

    return cell
}
Rayen Kamta
  • 115
  • 1
  • 2
  • 9

1 Answers1

0

I developed something similar, was adding view (with video) to a custom cell and call -updateVisibility method on -didScroll delegate of tableView

assuming that self here is a your view with video

    - (void)updateVisibilityInScrollView:(UIScrollView*)scrollView
    {
        if (!self.superview) {
            return;
        }
    // Get rect of video relative to scrollView (which is tableView)
        CGRect relativeToScrollViewRect = [self convertRect:self.bounds toView:scrollView];
// get visible scrollView rect
        CGRect visibleScrollViewRect = CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, scrollView.bounds.size.width, scrollView.bounds.size.height);

        if ([self moreThenHalfOfRect:relativeToScrollViewRect visibleInRect:visibleScrollViewRect]) {
            // visible half - resume
        } else {
            // supposed to be paused
        }
    }


- (BOOL)moreThenHalfOfRect:(CGRect)rect visibleInRect:(CGRect)visibleRect
{
    return (CGRectContainsPoint(visibleRect, CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect))));
}

Ofcaurce, you can modify to show video for fully visible cells

Sorry I just copied my solution (don't have swift example).

Injectios
  • 2,777
  • 1
  • 30
  • 50
  • func updateVisibility(in scrollView: UIScrollView) { if !superview { return } let relativeToScrollViewRect: CGRect = convert(bounds, to: scrollView) let visibleScrollViewRect = CGRect(x: scrollView.contentOffset.x, y: scrollView.contentOffset.y, width: scrollView.bounds.size.width, height: scrollView.bounds.size.height) if moreThenHalfOf(relativeToScrollViewRect, visibleIn: visibleScrollViewRect) { } else { }} func moreThenHalfOf(_ rect: CGRect, visibleIn visibleRect: CGRect) -> Bool {return visibleRect.contains(CGPoint(x: rect.midX, y: rect.midY)) } – Rayen Kamta Oct 17 '17 at 17:22