Created
May 30, 2020 19:57
-
-
Save Dimillian/6eff08819261f32911e0994e7748c452 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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