If I fetch a library playlist like the generated "Favorites" playlist via MusicKit like this
guard let initialTracks = try await playlist.with([.tracks]).tracks else {
return nil
}
I get a list of tracks like this:
...
TrackID: i.e5gmPS6rZ856
TrackID: i.4ZQMxU0OxNg0
TrackID: i.J198KH4P85K4
TrackID: i.J1AaRC4P85K4
TrackID: i.4BPqWt0OxNg0
TrackID: 4473570282773028026
TrackID: 4473570282773028025
TrackID: 4015088256684964387
TrackID: 4473570282773028024
TrackID: 7541557725362154249
TrackID: 4473570282773028027
I save the IDs for later use, but when I want to fetch them, only the ones with ids that starts with "i." work.
static func getLibrarySong(from id: String) async -> Song? {
var request = MusicLibraryRequest<Song>()
request.filter(matching: \.id, equalTo: MusicItemID(id))
do {
let response = try await request.response()
return response.items.first
} catch {
...
}
}
Or the Apple Music API endpoint :
static func getLibrarySongFromAPI(with id: String) async -> Song? {
guard let url = AppleMusicURL.getURL(for: .getSongById, id: id) else {
return nil
}
do {
let dataRequest = MusicDataRequest(urlRequest: URLRequest(url: url))
let dataResponse = try await dataRequest.response()
let response = try JSONDecoder().decode(SongsResponse.self, from: dataResponse.data)
return response.data.first
} catch {
...
}
}
Both functions above won't work for the non numeric like 4473570282773028024
so it seems the ID is wrong, but how do I make it work?
Otherwise I can fetch all the songs fine, in catalog or in the library, but these few songs can't be individually fetched, only with the try await
playlist.with([.tracks])` fetch, that gets the whole playlist. But obviously this isn't always possible.
Thanks in advance!