Sam
Sam

Reputation: 183

how to play track from certain position using spotify apps API

I am developing spotify third party application using javascript API. I am trying to play the track from certain position. I am having following code,

player.position = 50000; player.play(track);

But this code plays track from begining. I want to play the track from certain position. Is there any way to play track from certain position? Thanks.

Upvotes: 2

Views: 1444

Answers (3)

Nivek Mozart
Nivek Mozart

Reputation: 37

let currentPos = 1500; // in ms

function play() {

 let body = {};
 body.offset = {};
 body.context_uri = "spotify:playlist:" + playlistId; // play playlist
 body.offset.position = 0; // selects the first one in the playlist
 body.position_ms = currentPos; // in ms
 fetchApi("PUT", PLAY_API + "?device_id=" + deviceId, JSON.stringify(body));

}

This is the reference https://developer.spotify.com/documentation/web-api/reference/start-a-users-playback

Upvotes: 0

Martin Carlsson
Martin Carlsson

Reputation: 306

You can also link directly to a specific position in a track by adding #1:31 to the end of a URI, for example spotify:track:3iDK8BAaBUatPR84gdfa9g#0:15.

Upvotes: 2

iKenndac
iKenndac

Reputation: 18776

You need to play the track first, then as soon as playback starts seek to the location you want.

You can observe when playback starts by adding an observer for models.EVENT.CHANGE to the player object and looking at the playing property.

Upvotes: 4

Related Questions