0

I have a tableview and there are some cells that have videos in them . When a user gets to a cell that has a video then the video plays automatically . What I want is to have the video pause after your scrolling to the next cell. Right now when you are on a cell that has a video it plays but as soon as your scroll to the next cell the spot were the video was turns completely white (I have example images below) . My code is below, I am using swift 4 and the AVFoundation for the videos.

 // I call my custom method below
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    return homeProfilePlacesCell.HomeProfilePlaceTVC(tableView, cellForRowAt: indexPath, streamsModel: streamsModel, HOMEPROFILE: homeProfile, controller: self)

}

// This is a custom method since I have other TableViews that use this code

func HomeProfilePlaceTVC(_ tableView: UITableView, cellForRowAt indexPath: IndexPath, streamsModel : streamModel,HOMEPROFILE: HomeProfile, controller: UIViewController) -> UITableViewCell {




    let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTVC", for: indexPath) as! HomeTVC

 if filename.pathExtension == "mov" {
 // If a file has a 'mov' then it is a video and play it

             let movieURL = URL(string: streamsModel.stream_image_string[indexPath.row])
             // sets Video Height 
             cell.videoHeight.constant = CGFloat(Float(cell.video_height!))
             streamsModel.playerView = AVPlayer(url: movieURL!)
             streamsModel.MyAVPlayer.player = streamsModel.playerView
             streamsModel.MyAVPlayer.videoGravity = AVLayerVideoGravity.resizeAspectFill.rawValue
             streamsModel.MyAVPlayer.showsPlaybackControls = false
             streamsModel.MyAVPlayer.view.frame = cell.videoView.bounds
             cell.videoView.addSubview(streamsModel.MyAVPlayer.view)
             controller.addChildViewController(streamsModel.MyAVPlayer)
             streamsModel.playerView?.isMuted = false
             streamsModel.MyAVPlayer.player?.play() // play


        }


     }

    else {
        streamsModel.MyAVPlayer.player?.pause() // pause it

    }



    return cell
}

This is an example when you get to the cell the video plays automatically which is good [see second image below

enter image description here

This is how the video looks if you keep scrolling down, it turns into a big white blank spot where the video played. I instead want the video to be paused at this moment. I know other social networks can pause the video as users scroll down so I want to do the same.

enter image description here

user1591668
  • 2,591
  • 5
  • 41
  • 84
  • this link will help you https://stackoverflow.com/questions/33702490/embedding-videos-in-a-tableview-cell – Iraniya Naynesh Mar 08 '18 at 04:50
  • Possible duplicate of [UITableView Scroll event](https://stackoverflow.com/questions/8642699/uitableview-scroll-event) – El Tomato Mar 08 '18 at 05:01
  • for that use `didEndDisplayingCell` delegate. you get cell which is gone out of bound and you need to pause that video. – Kuldeep Mar 08 '18 at 06:44

1 Answers1

0

When the screen turns white like that, that means the AVPlayer has removed from the view. You can test this out by setting the background color of both the cell and self.view and seeing if it changes from white to that color. From the code you posted, I can't see why this is happening, so you might have to post more code or figure it out yourself.

To get the pause functionality, there's a delegate method didEndDisplaying https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614870-tableview, that tells when a cell has been moved off the screen. Here you will need an outside reference to your AVPlayer, and you'll do

MyAVPlayer.player?.pause()

Keep in mind, there is a sneaky limitation where IOS can only support 16 simultaneous instances of AVPlayer before introducing wonky behaviour (some videos randomly start flickering and turning black). If you run into this, then simply pausing the video will not be enough, and you'll have to create a buffer of 8 videos in both the up and down direction and manually allocate and deallocate the AVPlayers. Check out this post for more details Multiple AVPlayers in a UIScrollView - only 16 of them are shown

Aditya Garg
  • 838
  • 6
  • 19
  • 1
    I'd like to say that it's not AVPlayer instances but, AVPlayer + AVPlayerItem combos that will create that. – NSPunk Oct 10 '18 at 07:29