0

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.

rycirese
  • 179
  • 1
  • 14
  • Was the song explicit? I have had trouble getting MusicKitJS to playback anything that has a contentRating of explicit. – tomwilson Jun 11 '18 at 12:47
  • @tomwilson I can play explicit and non explicit songs with the first type of id. Neither play with the second type. – rycirese Jun 11 '18 at 13:09
  • 1
    Hm I managed to play things from my library with: music.api.library.albums().then((albums) => { music.setQueue({album: albums[0].id}).then(() => { music.play() }) }); – tomwilson Jun 12 '18 at 08:29
  • Thank you so much! I needed the `then()` on the `setQueue` or it wouldn't work. Calling play directly after setQueue didn't work. Odd that it worked for songs from the catalog, but I guess it makes sense since it has the ID it needs already, but not sure. – rycirese Jun 12 '18 at 13:44
  • This doesn't work if I try with: music.api.library.songs().then(songs => { music.setQueue({song: songs[0].id}).then(() => { music.play() }) }); – rycirese Jun 12 '18 at 13:50
  • Nice one! If only I could solve my issue with explicit songs :( – tomwilson Jun 12 '18 at 23:53
  • @tomwilson That's so strange. I've confirmed I can play explicit songs on my test setup. Are you able to `setQueue` for a single song and successfully play it? – rycirese Jun 13 '18 at 01:36
  • I can.. as long as the song has a contentRating field of empty or "clean". I actually opened a radar with apple and they actually responded asking for more information so will see how it goes – tomwilson Jun 13 '18 at 07:27
  • @tomwilson I got a single song to play if i used an array of that single song and used items instead of song when setting the queue. Try that maybe? – rycirese Jun 13 '18 at 17:36
  • Yeah I can get songs to play by passing an array but explicit songs still don't work. Also I just discovered if I queue up a playlist - only the non-explicit songs will be queued. I've had a bit more back and fourth with Apple they wanted to know which account I was using. I believe it might be related to something broken on my account OR the fact that I am connecting to the Australian region of Apple Music. – tomwilson Jun 14 '18 at 03:31

2 Answers2

0

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();
    });
});
Jae Hess
  • 141
  • 1
  • 5
  • MusicKitJS documentation is not complete, and in v3 beta I cannot force it to set whole playlist in the queue, or songs array. Do you know correct syntax? e.g. I try ` music.setQueue({playlist:'p.kKQrFRr11lQ'}).then((queue)=>{ music.play(); }) ` – Sebastian Jan 29 '22 at 13:02
  • @Sebastian I also am trying that and I am having no success with v3. Does anyone know how to queue and play songs with v3? – Alex Rabin May 15 '22 at 23:01
  • @AlexRabin It looks they do not implement option for playing playlist. If I find motivation maybe I correct that error. Playing single song or album is easy, unfortunately you must write whole app inside callback. – Sebastian May 17 '22 at 09:31
0

For @AlexRabin: I did playback when clicking on li element like this:

$('div.results').on("click",'li',function(){
  var index = $(this).index();
  
  console.log("li clicked "+index);
  var klasarodzica = $(this).parent().attr("class");
  let playParams = {};
  if (klasarodzica == "songs") {
     playParams = window.songsarray[index].attributes.playParams;
  }
  if (klasarodzica == "albums") {
     playParams = window.albumsarray[index].attributes.playParams;
  }

  var kind = playParams.kind;
  let id = playParams.id;
  let playobj ={[kind]:id};
  music.setQueue(playobj).then(function() {
    music.play();
  });

});

But as I said you have to write entire app in event listener, otherwise music object is unknown.

Sebastian
  • 448
  • 4
  • 14