From cb25148a90c73d808e052d305812f3c1f8281275 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Tue, 16 Aug 2022 17:09:22 +0800 Subject: [PATCH] Set playing info in media centre, fix bug with ImageCache --- apple/InfoPlists/Omnivore.plist | 1 + .../Services/AudioSession/AudioSession.swift | 40 +++++++++++++++++++ .../Sources/Utils/ImageCache.swift | 13 ++++-- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/apple/InfoPlists/Omnivore.plist b/apple/InfoPlists/Omnivore.plist index c9a47cec4..2495872c4 100644 --- a/apple/InfoPlists/Omnivore.plist +++ b/apple/InfoPlists/Omnivore.plist @@ -62,6 +62,7 @@ Images from your photo library can be chosen by you to send as feedback. UIBackgroundModes + audio fetch UILaunchStoryboardName diff --git a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioSession.swift b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioSession.swift index 4e1525444..b168a47e3 100644 --- a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioSession.swift +++ b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioSession.swift @@ -7,6 +7,7 @@ import AVFoundation import Foundation +import MediaPlayer import Models import Utils @@ -57,10 +58,12 @@ public class AudioSession: ObservableObject { self.audioUrl = Bundle.main.url(forResource: "speech-sample", withExtension: "mp3")! if let url = self.audioUrl { do { + try? AVAudioSession.sharedInstance().setCategory(.playback) self.player = try AVAudioPlayer(contentsOf: url) if self.player?.play() ?? false { self.state = .playing self.startTimer() + self.setupRemoteControl() } } catch { print(error.localizedDescription) @@ -110,4 +113,41 @@ public class AudioSession: ObservableObject { print("play time in ms: ", Int(player.currentTime * 1000)) } } + + func setupRemoteControl() { + UIApplication.shared.beginReceivingRemoteControlEvents() + + MPNowPlayingInfoCenter.default().nowPlayingInfo = [ + // MPMediaItemArtwork: ""m + MPMediaItemPropertyArtist: item?.author ?? "Omnivore", + MPMediaItemPropertyTitle: item?.title ?? "Your Omnivore Article" + ] + + if let imageURL = item?.imageURL, let cachedImage = ImageCache.shared[imageURL] { +// #if os(iOS) +// status = .loaded(image: Image(uiImage: cachedImage)) +// #else +// status = .loaded(image: Image(nsImage: cachedImage)) +// #endif + MPNowPlayingInfoCenter.default().nowPlayingInfo = [ + MPMediaItemPropertyArtwork: cachedImage, + MPMediaItemPropertyArtist: item?.author ?? "Omnivore", + MPMediaItemPropertyTitle: item?.title ?? "Your Omnivore Article" + ] + } + + let commandCenter = MPRemoteCommandCenter.shared() + + commandCenter.playCommand.isEnabled = true + commandCenter.playCommand.addTarget { _ -> MPRemoteCommandHandlerStatus in + self.unpause() + return .success + } + + commandCenter.pauseCommand.isEnabled = true + commandCenter.pauseCommand.addTarget { _ -> MPRemoteCommandHandlerStatus in + self.pause() + return .success + } + } } diff --git a/apple/OmnivoreKit/Sources/Utils/ImageCache.swift b/apple/OmnivoreKit/Sources/Utils/ImageCache.swift index 1e8e4416b..50868ecf7 100644 --- a/apple/OmnivoreKit/Sources/Utils/ImageCache.swift +++ b/apple/OmnivoreKit/Sources/Utils/ImageCache.swift @@ -27,16 +27,18 @@ public final class ImageCache { } private let queue = DispatchQueue(label: "app.omnivore.image.cache.queue", attributes: .concurrent) - private let cache = NSCache() + private let cache = NSCache() private init() { - cache.totalCostLimit = 1024 * 1024 * 50 // 50 MB + // cache.totalCostLimit = 1024 * 1024 * 1024 * 50 // 50 MB + print("CREATING IMAGE CACHE: ", self) } private func image(_ url: URL) -> PlatformImage? { var cachedImage: PlatformImage? queue.sync { - cachedImage = cache.object(forKey: url as AnyObject) + cachedImage = cache.object(forKey: NSString(string: url.absoluteString)) + print("CACHED IMAGE", cachedImage) } return cachedImage } @@ -44,7 +46,10 @@ public final class ImageCache { private func insertImage(_ image: PlatformImage?, url: URL) { guard let image = image else { return } queue.async(flags: .barrier) { - self.cache.setObject(image, forKey: url as AnyObject, cost: image.diskSize) + print("ADDING KEY: ", url, image) + self.cache.setObject(image, forKey: NSString(string: url.absoluteString), cost: 1) + let cachedImage = self.cache.object(forKey: NSString(string: url.absoluteString)) + print(" ---- JUST ADDED CACHED IMAGE: ", cachedImage, url) } } }