From 56441b59589a323ec6193ecbce29f5929e1a4391 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Tue, 13 Sep 2022 17:06:38 +0800 Subject: [PATCH] Download audio artwork --- .../AudioSession/AudioController.swift | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift index 4924dbc6a..09cecf14b 100644 --- a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift +++ b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift @@ -258,6 +258,7 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate if let itemID = itemAudioProperties?.itemID { Task { self.document = try? await downloadSpeechFile(itemID: itemID, priority: .high) + DispatchQueue.main.async { let synthesizer = SpeechSynthesizer(appEnvironment: self.appEnvironment, networker: self.networker, document: self.document!) self.durations = synthesizer.estimatedDurations(forSpeed: self.playbackRate) @@ -484,6 +485,26 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate MPNowPlayingInfoCenter.default().nowPlayingInfo = [:] } + func downloadAndSetArtwork() async { + if let pageId = itemAudioProperties?.itemID, let imageURL = itemAudioProperties?.imageURL { + if let result = try? await URLSession.shared.data(from: imageURL) { + if let downloadedImage = UIImage(data: result.0) { + let artwork = MPMediaItemArtwork(boundsSize: downloadedImage.size, requestHandler: { _ -> UIImage in + downloadedImage + }) + DispatchQueue.main.async { + if pageId == self.itemAudioProperties?.itemID { + if var nowPlaying = MPNowPlayingInfoCenter.default().nowPlayingInfo { + nowPlaying[MPMediaItemPropertyArtwork] = artwork + MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlaying + } + } + } + } + } + } + } + func setupRemoteControl() { UIApplication.shared.beginReceivingRemoteControlEvents() @@ -538,6 +559,10 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate } return .commandFailed } + + Task { + await downloadAndSetArtwork() + } } func downloadSpeechFile(itemID: String, priority: DownloadPriority) async throws -> SpeechDocument? {