Пользователь в приложении видит таблицу с аудио и видео файлами, приходящие от сервера, отсортированные по дате создания. Чтобы воспроизвести файл нужно нажать на ячейку. Задача: как сделать, чтобы после окончания проигрывания файла сразу же начиналось проигрывания следующего?
В UITableView при нажатии на ячейку реализуется в UIViewController
:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("indexPath.row:", indexPath.row)
let record = self.records[indexPath.row]
let mainTabBarController = UIApplication.shared.keyWindow?.rootViewController as? MainTabBarController
mainTabBarController?.minimizePlayerDetails(record: record)
tableView.deselectRow(at: indexPath, animated: true)
}
Воспроизведение реализуется в class PlayerDetailsView: UIView
:
fileprivate func playRecord () {
guard let url = URL(string: record.recordUrl) else { return }
let playItem = AVPlayerItem(url: url)
player.replaceCurrentItem(with: playItem)
NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playItem)
player.play()
}
@objc func playerDidFinishPlaying(note: NSNotification) {
// print("Player finished")
currentTimeSlider.setValue(0, animated: true)
currentTimeLabel.text = "00:00"
self.playPause.setImage(#imageLiteral(resourceName: "PlayPlayer"), for: .normal)
self.miniPlayPauseButton.setImage(#imageLiteral(resourceName: "PlayPlayerMini"), for: .normal)
let zero = CMTime(seconds: 0.0, preferredTimescale: 1)
player.seek(to: zero, toleranceBefore: zero, toleranceAfter: zero)
player.pause()
}