Player now plays episode objects! abstract away from urls
MODIFY
Podcast/DiscoverTableViewCell.swift
Podcast/DiscoverTableViewCell.swift
@@ -260,13 +260,8 @@ class DiscoverTableViewCell: UITableViewCell {
}
func clickToPlayImageButtonPress() {
- if let episode = self.episode {
- if let url = episode.mp3URL {
- Player.sharedInstance.prepareToPlay(url: url)
- Player.sharedInstance.play()
- }
- }
- return
+ guard let episode = episode else { return }
+ Player.sharedInstance.playEpisode(episode: episode)
}
}

MODIFY
Podcast/Player.swift
Podcast/Player.swift
@@ -48,14 +48,7 @@ class Player: NSObject {
oldValue?.pause()
}
}
- private var currentURL: URL? {
- didSet {
- if oldValue != currentURL {
- prepareToPlay(url: currentURL)
- }
- }
- }
-
+ private var currentEpisode: Episode?
deinit {
do {
try currentAVPlayer?.removeObserver(self, forKeyPath: #keyPath(AVPlayer.status), context: &playerContext)
@@ -64,16 +57,16 @@ class Player: NSObject {
removeEndOfItemObserver()
}
- func prepareToPlay(url: URL?) {
- if let url = url {
- if currentURL != url || playerStatus != .loading {
- // create a new player if this is a new URL or we aren't currently preparing a URL
+ func playEpisode(episode: Episode) {
+ if episode.mp3URL == nil || episode == currentEpisode || playerStatus == .loading {
+ // currently only support switching episodes when Player isn't already loading
+ return
+ }
+ currentEpisode = episode
playerStatus = .loading
- currentAVPlayer = AVPlayer(url: url)
+ currentAVPlayer = AVPlayer(url: episode.mp3URL!)
currentAVPlayer?.addObserver(self, forKeyPath: #keyPath(AVPlayer.status), options: [.old, .new], context: &playerContext)
}
- }
- }
func play() {
guard let player = currentAVPlayer else { return }

Player now plays episode objects! abstract away from urls