How to play music using Apple Music Kit JS

731 views Asked by At

I'm trying to create a small player using Apple Music Kit JS but I'm having issues when playing the song.

This is my current code:

<html>

<head>
  <meta charset=utf-8>
  <link rel="shortcut icon" href="#" />
  <title>Hear for Mac</title>
</head>

<body>

  <script src="https://js-cdn.music.apple.com/musickit/v1/musickit.js"></script>
  <script>
    const music = MusicKit.configure({
      developerToken: 'token', // I put my own token in the real code
      app: {
        name: 'Hear',
        build: '1978.4.1'
      }
    });

    music.authorize().then(function () {
      console.log("Authorized");
      music.setQueue({
        song: '1292397498'
      }).then(function () {
         music.play();
      })
    })

    window.music = music;
  </script>
</body>

</html>

And this is the error I'm getting when it tries playing:

TypeError: Argument 1 ('event') to EventTarget.dispatchEvent must be an instance of Event

1

There are 1 answers

0
Sebastian On

you forgot in the script first

document.addEventListener('musickitloaded',function() {
// here configuration of MusicKit Instance and all the playback
}

In time you are trying to configure MusicKit it is not yet loaded