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
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.