Swift Radio Pro现在正在播放专辑插图

时间:2019-02-21 21:47:06

标签: ios swift

大家好,我想从我们的电台RDS获取“正在播放”专辑的插图。我能够在添加的“最近播放”视图控制器中实现它。观看我附加的视频。 ScreenRecording

那么有什么方法可以让正在播放的视频从我们的工作站RDS提取图稿URL? 如果不是,那么如果当前正在播放某个歌手或专辑,是否有办法让正在播放的歌曲从URL中提取图像?原因是我们每天有来宾DJ混合一个小时,我们希望在混合时显示它们的图像。

谢谢大家

这是我当前从iTunes中提取专辑插图的代码:

    import Foundation

// MARK: - iTunes API
internal struct FRadioAPI {

    // MARK: - Util methods

    static func getArtwork(for metadata: String, size: Int, completionHandler: @escaping (_ artworkURL: URL?) -> ()) {

        guard !metadata.isEmpty, metadata !=  " - ", let url = getURL(with: metadata) else {
            completionHandler(nil)
            return
        }


        URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
            guard error == nil, let data = data else {
                completionHandler(nil)
                return
            }

            let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments)

            guard let parsedResult = json as? [String: Any],
                let results = parsedResult[Keys.results] as? Array<[String: Any]>,
                let result = results.first,
                var artwork = result[Keys.artwork] as? String else {
                    completionHandler(nil)
                    return
            }

            if size != 100, size > 0 {
                artwork = artwork.replacingOccurrences(of: "100x100", with: "\(size)x\(size)")
            }

            let artworkURL = URL(string: artwork)
            completionHandler(artworkURL)
        }).resume()
    }

    private static func getURL(with term: String) -> URL? {
        var components = URLComponents()
        components.scheme = Domain.scheme
        components.host = Domain.host
        components.path = Domain.path
        components.queryItems = [URLQueryItem]()
        components.queryItems?.append(URLQueryItem(name: Keys.term, value: term))
        components.queryItems?.append(URLQueryItem(name: Keys.entity, value: Values.entity))
        return components.url
    }

    // MARK: - Constants

    private struct Domain {
        static let scheme = "https"
        static let host = "itunes.apple.com"
        static let path = "/search"
    }

    private struct Keys {
        // Request
        static let term = "term"
        static let entity = "entity"

        // Response
        static let results = "results"
        static let artwork = "artworkUrl100"
    }

    private struct Values {
        static let entity = "song"
    }
}

0 个答案:

没有答案