2022-03-22 16:56:59 +01:00
|
|
|
//
|
|
|
|
// NowPlaying.swift
|
|
|
|
// App
|
|
|
|
//
|
|
|
|
// Created by Rasmus Krämer on 22.03.22.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import MediaPlayer
|
|
|
|
|
|
|
|
func getData(from url: URL, completion: @escaping (UIImage?) -> Void) {
|
|
|
|
URLSession.shared.dataTask(with: url, completionHandler: {(data, response, error) in
|
|
|
|
if let data = data {
|
|
|
|
completion(UIImage(data:data))
|
|
|
|
}
|
|
|
|
}).resume()
|
|
|
|
}
|
|
|
|
|
2022-04-12 14:28:47 +02:00
|
|
|
struct NowPlayingMetadata {
|
|
|
|
var id: String
|
2022-04-14 12:24:27 +02:00
|
|
|
var itemId: String
|
2022-04-12 14:28:47 +02:00
|
|
|
var artworkUrl: String?
|
|
|
|
var title: String
|
|
|
|
var author: String?
|
|
|
|
var series: String?
|
|
|
|
}
|
|
|
|
|
2022-03-22 16:56:59 +01:00
|
|
|
class NowPlayingInfo {
|
|
|
|
private static var nowPlayingInfo: [String: Any] = [:]
|
|
|
|
|
2022-04-14 11:54:53 +02:00
|
|
|
public static func setSessionMetadata(metadata: NowPlayingMetadata) {
|
2022-04-14 12:24:27 +02:00
|
|
|
setMetadata(artwork: nil, metadata: metadata)
|
2022-03-22 16:56:59 +01:00
|
|
|
|
2022-04-14 12:24:27 +02:00
|
|
|
/*
|
2022-04-12 14:28:47 +02:00
|
|
|
if !shouldFetchCover(id: metadata.id) || metadata.artworkUrl == nil {
|
2022-03-22 16:56:59 +01:00
|
|
|
return
|
|
|
|
}
|
2022-04-14 12:24:27 +02:00
|
|
|
*/
|
|
|
|
|
2022-04-15 10:16:11 +02:00
|
|
|
guard let url = URL(string: "\(Store.serverConfig!.address)/api/items/\(metadata.itemId)/cover?token=\(Store.serverConfig!.token)") else {
|
2022-04-14 12:24:27 +02:00
|
|
|
return
|
|
|
|
}
|
2022-03-22 16:56:59 +01:00
|
|
|
|
|
|
|
getData(from: url) { [self] image in
|
|
|
|
guard let downloadedImage = image else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let artwork = MPMediaItemArtwork.init(boundsSize: downloadedImage.size, requestHandler: { _ -> UIImage in
|
|
|
|
return downloadedImage
|
|
|
|
})
|
|
|
|
|
2022-04-12 14:28:47 +02:00
|
|
|
self.setMetadata(artwork: artwork, metadata: metadata)
|
2022-03-22 16:56:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public static func update(duration: Double, currentTime: Double, rate: Float) {
|
|
|
|
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = duration
|
|
|
|
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = currentTime
|
|
|
|
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = rate
|
|
|
|
nowPlayingInfo[MPNowPlayingInfoPropertyDefaultPlaybackRate] = 1.0
|
|
|
|
|
|
|
|
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
|
|
|
|
}
|
|
|
|
public static func reset() {
|
|
|
|
MPNowPlayingInfoCenter.default().nowPlayingInfo = nil
|
|
|
|
}
|
|
|
|
|
2022-04-12 14:28:47 +02:00
|
|
|
private static func setMetadata(artwork: MPMediaItemArtwork?, metadata: NowPlayingMetadata?) {
|
|
|
|
if metadata == nil {
|
2022-03-22 16:56:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if artwork != nil {
|
|
|
|
nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork
|
2022-04-12 14:28:47 +02:00
|
|
|
} else if shouldFetchCover(id: metadata!.id) {
|
2022-03-22 16:56:59 +01:00
|
|
|
nowPlayingInfo[MPMediaItemPropertyArtwork] = nil
|
|
|
|
}
|
|
|
|
|
2022-04-12 14:28:47 +02:00
|
|
|
nowPlayingInfo[MPNowPlayingInfoPropertyExternalContentIdentifier] = metadata!.id
|
2022-03-22 16:56:59 +01:00
|
|
|
nowPlayingInfo[MPNowPlayingInfoPropertyIsLiveStream] = false
|
|
|
|
nowPlayingInfo[MPNowPlayingInfoPropertyMediaType] = "hls"
|
|
|
|
|
2022-04-12 14:28:47 +02:00
|
|
|
nowPlayingInfo[MPMediaItemPropertyTitle] = metadata!.title
|
|
|
|
nowPlayingInfo[MPMediaItemPropertyArtist] = metadata!.author ?? "unknown"
|
|
|
|
nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = metadata!.series
|
2022-03-22 16:56:59 +01:00
|
|
|
}
|
2022-04-12 14:28:47 +02:00
|
|
|
private static func shouldFetchCover(id: String) -> Bool {
|
|
|
|
nowPlayingInfo[MPNowPlayingInfoPropertyExternalContentIdentifier] as? String != id || nowPlayingInfo[MPMediaItemPropertyArtwork] == nil
|
2022-03-22 16:56:59 +01:00
|
|
|
}
|
|
|
|
}
|