【发布时间】:2018-08-16 08:27:09
【问题描述】:
我有一个表格视图,其中一些单元格中有视频。当用户到达有视频的单元格时,视频会自动播放。 我想要的是在您滚动到下一个单元格后暂停视频。现在,当您在一个播放视频的单元格上时,但是一旦您滚动到下一个单元格,视频就会变成完全白色(我在下面有示例图像)。我的代码在下面,我正在使用 swift 4 和视频的 AVFoundation。
// 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
}
这是一个示例,当您到达单元格时,视频会自动播放,这很好[见下面的第二张图片
如果您继续向下滚动,这就是视频的外观,它会变成一个大的白色空白点,播放视频。相反,我希望此时暂停视频。我知道其他社交网络可以在用户向下滚动时暂停视频,所以我也想这样做。
【问题讨论】:
-
为此使用
didEndDisplayingCell代表。您得到的单元格超出范围,您需要暂停该视频。
标签: ios swift uitableview swift3 avfoundation