1

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.

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.

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" https://i.stack.imgur.com/rNevw.jpg

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"

document.addEventListener('musickitloaded', () => {
    console.log("musickit loaded :D")
  // MusicKit global is now defined
  fetch('/token').then(response => response.json()).then(res => {
    /***
      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', () => {
      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', () => {
      /***
        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', () => {
      /***
        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', () => {
        /***
         * 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', () => {
        /***
         * 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
    })
</head>
<body>
  <label>Type (eg 'album' 'song')</label>
  <input id="type-input" type="text" value="album">
  <br>
  <label>id</label>
  <input id="id-input" type="text" value="1462319084">
  <br>
  <button id="add-to-q-btn">Add to Queue</button>
  <br>
  <hr>
  <button id="prev-trk-btn">Prev Track</button>
  <button id="play-btn">Play</button>
  <button id="pause-btn">Pause</button>
  <button id="next-trk-btn">Next Track</button>

  <button id="login-btn">Login to Apple Music</button>
  <br>

I expect it to switch songs, play, pause, etc without showing any pop up errors. Maybe somehow handle these exceptions? Console output:

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

wpresti
  • 11
  • 1
  • 1

0 Answers0