Skip to content

Instantly share code, notes, and snippets.

@Dimillian
Created May 30, 2020 19:57
Show Gist options
  • Save Dimillian/6eff08819261f32911e0994e7748c452 to your computer and use it in GitHub Desktop.
Save Dimillian/6eff08819261f32911e0994e7748c452 to your computer and use it in GitHub Desktop.
public class MusicPlayerManager: ObservableObject {
public static let shared = MusicPlayerManager()
public enum PlayMode {
case random, ordered
}
@Published public var songs: [String: Song] = [:] {
@Published public var currentSong: Song? {
didSet {
if let song = currentSong {
let musicURL = ACNHApiService.makeURL(endpoint: .music(id: song.id))
player?.pause()
player = AVPlayer(url: musicURL)
}
}
}
@Published public var isPlaying = false {
didSet {
isPlaying ? player?.play() : player?.pause()
}
}
@Published public var playmode = PlayMode.random
@Published public var duration = "0:00"
@Published public var timeElasped = "0:00"
@Published public var playProgress: Float = 0
private var songsCancellable: AnyCancellable?
private var player: AVPlayer?
private var timeTimer: Timer?
init() {
songsCancellable = ACNHApiService
.fetch(endpoint: .songs)
.replaceError(with: [:])
.eraseToAnyPublisher()
.subscribe(on: DispatchQueue.global())
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] songs in
self?.songs = songs
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment