Has anyone figured this out yet?
I'm trying to use the library id (e.g. l.NP4oWSo) to play my library content from the Apple Music API (e.g. /v1/me/library/albums). I want to be consistent in using the MusicKit APIs vs using an MPMediaItemCollection, which is what is referenced in the Adding Content to Apple Music example referenced by 3ZS above.
The most consistent way to handle content coming from either the store catalog or the user's library is to use the Play Params returned from MusicKit, which contain metadata about the album or track, including id (l.NP4oWSo format for library items and an larger Integer for catalog items) and even an isLibrary key that is set to true for library items.
So the playParams from MusicKit for a library album item look like:
{
"id": "l.JmwYn9g",
"kind": "album",
"isLibrary": true
}
So my current thinking has been to try and create an MPMusicPlayerPlayParametersQueueDescriptor using the above play param [String: Any] dictionary.
From Apple's own documenatation on this object:
A set of properties and methods used to modify how items are played, based on play parameters returned from MusicKit API results. Use this class to modify the player queue created by a query before the queue begins to play. You can modify when individual items start and stop playing, along with setting the first item to be played.
So this seems like it should, work. I should be able to pass any play parameters from the Apple MusicKit api, per this documentation, and it will take those parameters and figure out how to set the queue and play the item. My code looks like:
let playParams: [String: Any] = ... // from MusicKit api object attributes/playParams
guard let playerPlayParams = MPMusicPlayerPlayParameters(dictionary: playParams) else {
// handle error if play params can't be parsed by initializer
return;
}
let queue = MPMusicPlayerPlayParametersQueueDescriptor(playParametersQueue: [playerPlayParams])
let musicPlayerController = MPMusicPlayerController.systemMusicPlayer
musicPlayerController.setQueue(with: queue)
(musicPlayerController as MPMediaPlayback).play()
Alas, this doesn't work. If there's no items in the queue already, it will not play anything, and if there are existing queue items it will not modify them before playback (e.g. plays the current queue).
I really want to work with this API, but so far the documentation has been very lacking and from this thread it seems like this library vs catalog playback issue is very confusing to anyone who is wanting to create awesome Apple Music apps.
I hope someone has figured this out, or someone from Apple will reply to this thread with more info as I've spent a lot of time trying to figure this out and have spent a lot of time documenting the issues in this post. The current example is very lacking in doing anything other than basic playback from the device library or playback of catalog requests (e.g. recently played api queries or search api queries). For those of us wanting to provide users of our MusicKit apps a great experience that references a user's music library in addition to just the catalog, we need to figure out a real way to handle this issue.
And just my 2 cents, as feedback the real differentiator of something like Apple Music vs Spotify, for me anyway, is the ability to have library items alongside streaming catalog items (e.g. stuff you have stored on iTunes Match) and so the inability to work with this via the api in an effective way is frustrating.
I am not able to play the song after 30 seconds i am getting preview url in the api not the full song url is there any update on this ? Thanks