javascriptapple-musicapple-musickit

MusicKitJS: Can't play songs from user's library


So I'm experimenting with Apple's MusicKit JS (https://developer.apple.com/documentation/musickitjs) that was released last week. I'm having trouble playing songs fetched from the library endpoints and from what I can tell it's because of the id format.

If I use their doc examples and set the queue to an album with an id like 1025210938, songs play fine. However when getting a song from a user's iCloud library (ie. /v1/me/library/albums or in musickitjs case music.api.library.albums()) I get an id that looks like l.uUZAkT3 and those do not do anything when I try to play them.

Maybe something who is more familiar with how Apple Music's API works in general or used MusicKit for iOS would be able to let me know why this is or how to get a usable id for a user's library items.


Solution

  • Most of MusicKit JS is based on Promises. The setQueue method is one of these Promise based methods.

    In particular setQueue can fetch the data for you, if you do not already have an API data response handy.

    In order to ensure the data is ready to be used, you would want to execute any playback functionality in the resolved Promise.

    MusicKit.getInstance().setQueue({ album: 'l.abc123'}).then(function(queue) {
        MusicKit.getInstance().play();
    });
    

    or

    MusicKit.getInstance().api.library.albums().then(function(albums) {
        MusicKit.getInstance().setQueue(albums[0]).then(function(queue) {
            MusicKit.getInstance().play();
        });
    });