Announcements

Help Wizard

Step 1

NEXT STEP

TypeError: Cannot add negative msPlayed value

TypeError: Cannot add negative msPlayed value

Im bring to use the web api to play songs (one by one - not playlist)

and I get the following at the html.

 
[Error] TypeError: Cannot add negative msPlayed value: -9
(anonymous function) (index.js:18:264819)
(anonymous function) (index.js:18:259217)
(anonymous function) (index.js:18:219226)
(anonymous function)
[Error] Unhandled Promise Rejection: TypeError: Cannot add negative msPlayed value: -10
(anonymous function)
rejectPromise
promiseReactionJob
 
can you please help?
Reply
8 Replies

Hey @OBO1, thanks for reporting this!

Hmm, could you send over a bit of your code? What endpoint are you using? I'll try to do some digging.

Thanks, keep me posted!
Hubo
HuboSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

Hi @Hubo,
Thanks for your response !

 

First of all ill mention that this only occurs on Safari.

 

I use the Web API to play tracks by their ids.

I'm using a code that is based on the docs.

 

 

 

 

var accessToken //Being updated every 45 minuts with a refresh_token request

 

 

 

 

 

 

 

window.onSpotifyWebPlaybackSDKReady = () => { ...
 player = new Spotify.Player({
                    name: 'Votify',
                    getOAuthToken: cb => { cb(accessToken); }
                });
 listeners ... 
}

 

 

 

 

 

 

 

Function loadTrack(currentTrack) {
   //Fetches the track from the database...
   if (player) {
       playOnSpotify({
          spotify_uri: currentTrack.uri,
          playerInstance: player
       });
}

 

 

 

 

and the playOnSpotify is:

 

 

 

 const playOnSpotify = ({
                spotify_uri,
                playerInstance: {
                    _options: {
                        getOAuthToken,
                        device_Id
                    }
                }
            }) => { 
                console.log('playOnSpot ==>' + spotify_uri + ', deviceId =>>> ' + deviceId)

                getOAuthToken(access_token => {
                    console.log('getOAuthToken ==>' + access_token)

                    fetch(`https://api.spotify.com/v1/me/player/play?device_id=${deviceId}`, {
                        method: 'PUT',
                        body: JSON.stringify({ uris: [spotify_uri] }),
                        headers: {
                            'Content-Type': 'application/json',
                            'Authorization': `Bearer ${access_token}`
                        },
                    })
                });
            };

 

 

 

 

I see this on Mac / Safari too. I almost never see it on Chrome. Steps to reproduce fairly reliably here:

 

- in Safari desktop or mobile initialize the web playback SDK and start a song per the docs

- See player_state_changed events as expected

- on Spotify Desktop see that a song is playing in the web playback device and advance the song position towards the end of the track

- Get `Unhandled Promise Rejection: TypeError: Cannot add negative msPlayed value: -5`

- Don't see any more player_state_changed events as expected

- Playing a new track through the web SDK also gives the TypeError

To clarify, you don't have to advance the track position in Spotify Desktop, it's just that the error happens at the end of a track so this is a faster way to trigger it.

I can reproduce the same error here on Mac/Safari 15.2 and iOS 15.1/Safari (Chrome works flawlessly). I built a simple album player, and when the tracks position reaches its end, the unhandled promise rejection pops up (not always, but most of the time). The source of this error is to be found in the file https://sdk.scdn.co/embedded/index.js on line 9819/9820. Maybe the solution for the Spotify developers would be as simple as changing line 9820 to e = 0; ? 🤔

I see this on Mac 12.0.1 / Firefox Dev Edition 96.0b10 (64-bit) too.

 

Seems to occur about 1 of 5 times that I call the `seek` method of the player.

I am also seeing this error on iOS & MacOS Safari when the current track finishes and you send a request to play the next track. I am playing tracks individually rather than from a playlist.

 

Is there a solution to this?

I'm also seeing this issue on the latest macOS with Safari.

Suggested posts