Code Block swift| import UIKit |
| import AVKit |
| import AVFoundation |
|
| class PodcastViewCell: UICollectionViewCell { |
|
| // UILabels |
| @IBOutlet weak var categoryLabel: UILabel! |
| @IBOutlet weak var titleLabel: UILabel! |
| @IBOutlet weak var dateLabel: UILabel! |
| @IBOutlet weak var podcastUnavailableLbl: UILabel! |
| |
| // Constraints |
| @IBOutlet weak var linehg: NSLayoutConstraint! |
| @IBOutlet weak var categoryLabelHeight: NSLayoutConstraint! |
| @IBOutlet weak var podcastBtnWd: NSLayoutConstraint! |
| |
| // UISliders |
| @IBOutlet weak var slider: UISlider! |
| |
| // UIViews |
| @IBOutlet weak var podcastView: UIView! |
| |
| // UIButtons |
| @IBOutlet weak var playPauseBtn: UIButton! |
| @IBOutlet weak var podcastBtn: UIButton! |
|
| var player: AVPlayer? |
| var playerItem: AVPlayerItem? |
| var timeObserverToken: Any? |
| |
| var urlString: String? |
| var podcastLink: String? |
| |
| var played = false |
| |
| |
| override func awakeFromNib() { |
| super.awakeFromNib() |
| podcastViewVisibility() |
| slider.value = 0 |
| } |
| |
| var item: News? { |
| didSet { |
| if let news = item { |
| titleLabel.text = news.title ?? "" |
| categoryLabel.text = news.category?.title ?? "" |
| dateLabel.text = news.publishDate ?? "" |
| } |
| } |
| } |
|
| func prepareCell(news: News?) { |
| item = news |
| } |
| |
| func prepareCellAuthor(author: AuthorSubList?) { |
| itemAuthor = author |
| } |
| |
| var itemAuthor: AuthorSubList? { |
| didSet { |
| if let column = itemAuthor { |
| titleLabel.text = column.title ?? "" |
| categoryLabel.text = "" |
| categoryLabelHeight.constant = 0 |
| dateLabel.text = column.publishDate ?? "" |
| } |
| } |
| } |
| |
| @IBAction func selectItem(_ sender: Any) { |
| if let news = item { |
| if let id = news.itemId { |
| delegate?.selectItemAction(itemId: id) |
| delegate?.selectPopularNews(news: news, index: tag) |
| } |
| } |
| |
| else if let author = itemAuthor { |
| if let id = author.itemId { |
| delegate?.selectItemAction(itemId: id) |
| } |
| } |
| } |
|
| @IBAction func playPodcast(_ sender: Any) { |
| |
| if let collectionView = self.superview as? UICollectionView { |
| let visibleCells = collectionView.visibleCells |
| for cell in visibleCells where cell is PodcastViewCell { |
| if cell.player != nil { } |
| } |
| } |
|
| if played == false { |
| NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil) |
| |
| if let itemOffset = allItems?.itemListv2.firstIndex(where: {$0.itemId == itemAuthor?.itemId}) { |
| podcastLink = allItems?.itemListv2[itemOffset].podcastsound |
| } |
|
| let url = podcastLink ?? " " |
| |
| if url != "" { |
| let playerItem = AVPlayerItem( url:NSURL( string:url )! as URL ) |
| player = AVPlayer(playerItem:playerItem) |
| player!.rate = 1.0; |
| |
| podcastView.isHidden = false |
| |
| player!.play() |
| playPauseBtn.setImage(UIImage(systemName: "pause.fill"), for: .normal) |
| |
| player?.addProgressObserver { progress in |
| self.slider.setValue(Float(progress), animated: true) |
| } |
| played = true |
| } else { |
| podcastUnavailableLbl.isHidden = false |
| } |
| } else { |
| podcastView.isHidden = true |
| player?.replaceCurrentItem(with: nil) |
| slider.value = 0 |
| played = false |
| } |
| } |
|
| deinit { |
| NotificationCenter.default.removeObserver(self) |
| } |
| |
| @IBAction func playPause(_ sender: Any) { |
| if player?.timeControlStatus == .playing { |
| player?.pause() |
| playPauseBtn.setImage(UIImage(systemName: "play.fill"), for: .normal) |
| } else { |
| player?.play() |
| playPauseBtn.setImage(UIImage(systemName: "pause.fill"), for: .normal) |
| } |
| } |
| } |
|
| extension AVPlayer { |
| func addProgressObserver(action:@escaping ((Double) -> Void)) -> Any { |
| return self.addPeriodicTimeObserver(forInterval: CMTime.init(value: 1, timescale: 1), queue: .main, using: { time in |
| if let duration = self.currentItem?.duration { |
| let duration = CMTimeGetSeconds(duration), time = CMTimeGetSeconds(time) |
| let progress = (time/duration) |
| action(progress) |
| } |
| }) |
| } |
| } |
| |