title: Unhandled Promise Rejection: PLAY_ACTIVITY -- Apple MusicKitJS API
body: <p>Im trying to build a simple apple music web app player. I successfully got a little example working. I'm able to load an album, song, etc to a the music player queue based off of the 'type' and 'id'. Im able to play, pause, skip to next track, previous track, etc but for some reason either when one song finishes and the next one starts (handled automatically by the API player queue) or I click skip to next track, pause, etc it will do what its suppose to do but it pops up with an error.</p>
  
  <p>Ex: I load an album to the queue. Click play. It starts playing music but sometimes it will randomly pop up with an error message as soon as it starts playing and print the error to the console. </p>
  
  <p>This is what shows up in the dev console. "Unhandled Promise Rejection: PLAY_ACTIVITY: A play stop() method was called without a previous play() descriptor"  <a href="https://imgur.com/5MCq08s" rel="nofollow noreferrer">https://imgur.com/5MCq08s</a></p>
  
  <p>I've tried having the js pause the song before switching tracks but that did not fix the issue. "Unhandled Promise Rejection: PLAY_ACTIVITY: A play stop() method was called without a previous play() descriptor" </p>
  
  <pre><code>document.addEventListener('musickitloaded', () =&gt; {
      console.log("musickit loaded :D")
    // MusicKit global is now defined
    fetch('/token').then(response =&gt; response.json()).then(res =&gt; {
      /***
        Configure our MusicKit instance with the signed token from server, returns a configured MusicKit Instance
        https://developer.apple.com/documentation/musickitjs/musickit/musickitinstance
      ***/
      const music = MusicKit.configure({
        developerToken: res.token,
        app: {
          name: 'AppleMusicCS115',
          build: '0.0.1'
        },
        declarativeMarkup: true
      });
  
      // setup click handlers
      document.getElementById('add-to-q-btn').addEventListener('click', () =&gt; {
        const idInput   = document.getElementById('id-input');
        const typeInput = document.getElementById('type-input');
  
        /***
          Add an item to the playback queue
          https://developer.apple.com/documentation/musickitjs/musickit/musickitinstance/2992716-setqueue
        ***/
        music.setQueue({
          [typeInput.value]: idInput.value
        });
        // set fields back to blank fields
        idInput.value   = '';
        typeInput.value = '';
      });
  
      document.getElementById('play-btn').addEventListener('click', () =&gt; {
        /***
          Resume or start playback of media item
          https://developer.apple.com/documentation/musickitjs/musickit/musickitinstance/2992709-play
        ***/
       music.authorize().then(function() {
          music.play();
        });// added then function
      });
  
      document.getElementById('pause-btn').addEventListener('click', () =&gt; {
        /***
          Pause playback of media item
          https://developer.apple.com/documentation/musickitjs/musickit/musickitinstance/2992708-pause
        ***/
       music.authorize().then(function() {
          music.pause();
        });// added then function
      });
  
      document.getElementById('next-trk-btn').addEventListener('click', () =&gt; {
          /***
           * Skip to next item in queue.
           * https://developer.apple.com/documentation/musickitjs/musickit/player/2992772-skiptonextitem
           */
          music.authorize().then(function() {
              music.skipToNextItem();
            });// added then function
  
  
  
      });
  
      document.getElementById('prev-trk-btn').addEventListener('click', () =&gt; {
          /***
           * https://developer.apple.com/documentation/musickitjs/musickit/player/2992773-skiptopreviousitem
           */
  
          music.authorize().then(function() {
              music.pause();;
            });
          music.authorize().then(function() {
              music.skipToPreviousItem();;
            });// added then function
      })
  </code></pre>
  
  <pre><code>&lt;/head&gt;
  &lt;body&gt;
    &lt;label&gt;Type (eg 'album' 'song')&lt;/label&gt;
    &lt;input id="type-input" type="text" value="album"&gt;
    &lt;br&gt;
    &lt;label&gt;id&lt;/label&gt;
    &lt;input id="id-input" type="text" value="1462319084"&gt;
    &lt;br&gt;
    &lt;button id="add-to-q-btn"&gt;Add to Queue&lt;/button&gt;
    &lt;br&gt;
    &lt;hr&gt;
    &lt;button id="prev-trk-btn"&gt;Prev Track&lt;/button&gt;
    &lt;button id="play-btn"&gt;Play&lt;/button&gt;
    &lt;button id="pause-btn"&gt;Pause&lt;/button&gt;
    &lt;button id="next-trk-btn"&gt;Next Track&lt;/button&gt;
  
    &lt;button id="login-btn"&gt;Login to Apple Music&lt;/button&gt;
    &lt;br&gt;
  </code></pre>
  
  <p>I expect it to switch songs, play, pause, etc without showing any pop up errors. Maybe somehow handle these exceptions?
  Console output: </p>
  
  <p>Unhandled Promise Rejection: 
  PLAY_ACTIVITY: A play stop() method was called without a previous play() descriptor
  (anonymous function) — musickit.js:1:14539
  rejected — musickit.js:534
  promiseReactionJob</p>