Set playing info in media centre, fix bug with ImageCache

This commit is contained in:
Jackson Harper
2022-08-16 17:09:22 +08:00
parent d918b8b0a8
commit cb25148a90
3 changed files with 50 additions and 4 deletions

View File

@ -62,6 +62,7 @@
<string>Images from your photo library can be chosen by you to send as feedback.</string>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>fetch</string>
</array>
<key>UILaunchStoryboardName</key>

View File

@ -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
}
}
}

View File

@ -27,16 +27,18 @@ public final class ImageCache {
}
private let queue = DispatchQueue(label: "app.omnivore.image.cache.queue", attributes: .concurrent)
private let cache = NSCache<AnyObject, PlatformImage>()
private let cache = NSCache<NSString, PlatformImage>()
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)
}
}
}