У меня есть локальный видеофайл, воспроизводимый в цикле с использованием AVPlayerLooper, но всякий раз, когда я нажимаю кнопку «Домой» (приложение входит в фоновый режим), а затем снова открываю приложение, воспроизведение видео прекращается. У меня также есть AVPlayerLooper внутри простого представления, а не collectionViewCell. Чтобы решить эту проблему, я просто вызываю player.play()
внутри applicationDidBecomeActive()
. Это не работает для collectionViewCell.
Попытка удалить playerLayer до того, как приложение войдет в фон, и добавить его снова, когда приложение активируется без успеха.
Код установки VideoView:
func setupPlayerView() {
if let videoURL = allExercises.array[currentExercise].videoLink {
if player == nil {
player = AVQueuePlayer()
player?.isMuted = true
playerLayer = AVPlayerLayer(player: player)
self.layer.addSublayer(playerLayer)
playerLayer.frame = self.frame
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
}
theItem = AVPlayerItem(url: URL(fileURLWithPath: videoURL))
playerLooper = AVPlayerLooper(player: player!, templateItem: theItem!)
player?.play()
print("DIDSETUP")
}
}
collectionView cellForItemAt:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
currentExercise = indexPath.section
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LiveView", for: indexPath) as! HorizontalLiveViewCell
cell.nameLabel.text = allExercises.array[indexPath.section].name
return cell
}
Переменная VideoView для collectionViewCell:
var videoView: VideoPlayerView {
let video = VideoPlayerView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width * (9 / 16)))
return video
}
Все работает идеально, пока я не нажму кнопку «Домой», отправляя приложение в фоновый режим. Как я могу перезапустить видео? Можно перезагружать всю ячейку, что не составит труда.