从收藏视图切换视频时,Swift 播放器冻结但音频仍然在后台运行
Posted
技术标签:
【中文标题】从收藏视图切换视频时,Swift 播放器冻结但音频仍然在后台运行【英文标题】:Swift player freezes but audio still works in background when switching videos from collection view 【发布时间】:2018-03-05 02:05:18 【问题描述】:这是我的意思的快速视频:https://youtu.be/Ld8apahZBIg
我有一个收藏视图,当我点击任何第一个视频播放时,它会完美显示。当我单击返回关闭播放器/视图以播放不同的视频时,音频在后台运行,但在播放视频时视频显示似乎冻结。
这是 UICollectionView 的相关代码:
class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout
override func viewDidLoad()
super.viewDidLoad()
collectionView?.register(HomePostCell.self, forCellWithReuseIdentifier: cellId)
collectionView?.register(HomeControllerHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HomeControllerHeaderId")
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
return posts.count
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
return 1
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
return 1
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! HomePostCell
// self.hpc = cell
cell.post = posts[indexPath.item]
return cell
var avPlayerViewController = VideoPlayerViewController()
var avPlayer = AVPlayer()
var playerLayer: AVPlayerLayer!
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
let links = posts[indexPath.row]
guard let url = NSURL(string: links.videoUrl) else return
var player = avPlayerViewController.player
player = AVPlayer(url: url as URL)
let playerItem = AVPlayerItem(url: url as URL)
avPlayer.replaceCurrentItem(with: playerItem)
let playerController = avPlayerViewController
playerController.player = player
self.navigationController?.pushViewController(playerController, animated: true)
playerController.player.play()
这是我的 avPlayer View Controller 类的相关代码:
class VideoPlayerViewController: UIViewController
var player: AVPlayer!
var playerLayer: AVPlayerLayer!
let videoView: UIView =
let vidView = UIView()
return vidView
()
override func viewDidLoad()
super.viewDidLoad()
playerLayer = AVPlayerLayer(player: player)
videoView.layer.addSublayer(playerLayer)
view.addSubview(videoView)
videoView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
override func viewDidAppear(_ animated: Bool)
super.viewDidAppear(true)
playerLayer.frame = view.bounds
self.view.layer.addSublayer(playerLayer)
player!.play()
override func viewDidLayoutSubviews()
super.viewDidLayoutSubviews()
playerLayer.frame = view.bounds
override func viewDidDisappear(_ animated: Bool)
player!.pause()
player = nil
playerLayer.removeFromSuperlayer()
如果您还有其他问题,请告诉我。
【问题讨论】:
"我有一个集合视图" 你的代码中没有一行建议这样做。 @ElTomato 好的,我添加了集合视图类 【参考方案1】:修复它。问题是我正在调用播放器来显示视频一次。每次我尝试删除子层并播放新视频时,视频播放器都会冻结,因为会发生这种情况,因为播放器层只会加载一次,然后当尝试播放新的播放器层时它不会因为它需要再次加载.每次显示视图控制器时,我都使用 viewDidAppear 加载内容。
class VideoPlayerViewController: UIViewController
var hc: HomeController!
var player: AVPlayer!
var playerLayer: AVPlayerLayer!
let videoView: UIView =
let vidView = UIView()
return vidView
()
override func viewDidLoad()
super.viewDidLoad()
view.backgroundColor = .black
override func viewDidAppear(_ animated: Bool)
super.viewDidAppear(true)
playerLayer = AVPlayerLayer(player: player)
view.addSubview(videoView)
videoView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
self.view.layer.addSublayer(playerLayer)
playerLayer.frame = view.bounds
player!.play()
override func viewDidDisappear(_ animated: Bool)
player!.pause()
player = nil
playerLayer.removeFromSuperlayer()
【讨论】:
嗨,我正在使用collectionviewcell面临同样的问题有什么帮助吗?以上是关于从收藏视图切换视频时,Swift 播放器冻结但音频仍然在后台运行的主要内容,如果未能解决你的问题,请参考以下文章
iOS Swift 在视图关闭时停止 MoviePlayer
MPMoviePlayerController iOS 播放音频文件问题
搜索文件时 MPMoviePlayer 播放音频但不播放视频
Swift 4 - 在 mac os 上使用 AVAssetWriter 进行 avfoundation 屏幕和音频录制 - 视频冻结