I am aware that ApplicationMusicPlayer.shared.state is implemented as an observed object, but I'm wondering if it's possible to store that instance in a class annotated with ObservableObject so that I can use objectWillChance or other properties and then forward the values and state I care about view @Published properties. I'm thinking something like this:
class SomeViewModel: ObservableObject {
@Published var isPlaying: Bool = false
init() {
ApplicationMusicPlayer.shared.state.objectWillChange.sink { [self]
isPlaying = ApplicationMusicPlayer.shared.state.playbackStatus == .playing
}
}
}
If I create this class and stick in a SwiftUI View I can see the init being called, but the objectWillChange never seems to fire.
I can however do this and it seems to work inside of the SwiftUI View:
struct SomeView: View {
@ObservedObject private var playerState = ApplicationMusicPlayer.shared.state
@State private var isPlaying: Bool = false
var cancellableBag = Set<AnyCancellable>()
init() {
self.isPlaying = playerState.playbackStatus == .playing
playerState.objectWillChange.sink { [self] in
print(playerState.playbackStatus)
}.store(in: &cancellableBag)
}
}
interesting objectWillChange fires twice for every state update, but I could deal with that if I could just use my own ObservableObject. I'd rather my music player not be so tightly coupled to the view I'm writing.
Post
Replies
Boosts
Views
Activity
In this thread it has been stated that getting artist artwork through the MusicKit api isn't allowed because of copyright issues. My question is:
Is using the Apple Music REST api to get album artwork via the OpenGraph API also disallowed such that my app would be rejected? Wonder if I should pursue this option that I found or if I need to find another way? Seems like the answer would be no, but wanted to double check.
Thanks for your response, this is my first app that I'm trying to publish to the store and I'd like to avoid getting an instant rejection for something like this.
Code snippet:
import MusicKit
import Foundation
import OpenGraph
extension Artist {
func artwork(width: Int = 1024, height: Int = 1024) async -> URL? {
let id = self.id.rawValue
let countryCode = try? await MusicDataRequest.currentCountryCode
let url = URL(string: "https://music.apple.com/\(countryCode ?? "us")/artist/\(id)")!
return await withCheckedContinuation { continuation in
OpenGraph.fetch(url: url) { result in
switch result {
case .success(let og):
let image = og[.image]?.resizeArtwork(width: width, height: height)
if let image = image, let url = URL(string: image) {
continuation.resume(returning: url)
} else {
continuation.resume(returning: nil)
}
case .failure(_):
continuation.resume(returning: nil)
}
}
}
}
}
extension String {
func resizeArtwork(width: Int, height: Int) -> String? {
do {
let regex = try NSRegularExpression(pattern: "/\\d+x\\d+cw", options: .caseInsensitive)
let newImage = regex.stringByReplacingMatches(in: self, options: [], range: NSRange(0..<self.utf16.count), withTemplate: "/\(width)x\(height)cc")
return newImage
} catch {
return nil
}
}
}