My view controller for podcasts table, now it plays the podcast too.
swift
class DetailViewController: BaseViewController {
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var podcastPlayerView: UIView!
var podcastLink: String = ""
static var itemId = "0"
var player: AVPlayer?
var playerItem: AVPlayerItem?
var timeObserverToken: Any?
var played = false
override func viewDidLoad() {
super.viewDidLoad()
"self.podcastPlayerView.isHidden = false" "-------- Unhiding process successful in this step"
}
override func viewWillDisappear(_ animated: Bool) {
showPodcastButton = false
player?.pause()
player?.replaceCurrentItem(with: nil)
player = nil
}
func setupPodcastPlayer(link: String) {
player?.pause()
player?.replaceCurrentItem(with: nil)
player = nil
if !played {
if link != "" {
playerItem = AVPlayerItem( url:NSURL( string:link )! as URL )
player = AVPlayer(playerItem:playerItem)
player!.rate = 1.0;
player!.play()
played = true
didPlayedOnce = true
podcastPlay()
} else {
"link empty"
}
} else {
player?.replaceCurrentItem(with: nil)
played = false
}
}
func podcastPlay() {
self.podcastPlayerView.isHidden = false "---- If I try to unhide here, app crashes."
"Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value"
}
}
The "Play Button" action from PodcastViewCell, where I call ViewController's player function. This only passes the cell's podcast link.
swift
@IBAction func playPodcast(_ sender: Any) {
NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
if let itemOffset = DetailViewController.allItems?.itemListv2.firstIndex(where: {$0.itemId == itemAuthor?.itemId}) {
podcastLink = DetailViewController.allItems?.itemListv2[itemOffset].podcastsound
}
let url = podcastLink ?? " "
requestAuthorDetailViewController.setupPodcastPlayer(link: url)
}
Topic:
UI Frameworks
SubTopic:
UIKit
Tags: