Music Kit JS SeekToTime

I'm trying to use the seekToTime instance method as shown here: https://developer.apple.com/documentation/musickitjs/musickit/player/2992768-seektotime

I don't know what "time" is, is this in seconds or milliseconds? I assume it's in seconds but I figure I'd ask anyway.

I am currently using it in this way:

  await musicKit.play();
  await musicKit.seekToTime(10);

This technically works, although it plays first from the start and then abruptly changes to play based on the "time" provided in the seekToTime method.

I tried using the seek method before playing but that throws an error. Same thing if I "chain" these together using seekToTime first "then" play the music.

Is there a "smoother" way to start playing at a specific time without this abrupt change?

Answered by Media Engineer in 717398022

Hello, @gonzosan, and thank you for the question!

seekToTime() does indeed take seconds, as a Number, as the argument. This method requires there to be a nowPlayingItem, which is set when playback starts, which is likely why you are seeing an error when you attempt to seek before calling play().

Chaining the Promise is expected to behave the same as the await syntax you’re using above, so it would also make sense for that to not impact the behavior.

In MusicKit on the Web v1, which is the version documented in the link from your post, there is not a better way to seek to a specific time before playback starts.

In MusicKit on the Web v3, there is a startTime option in the Object passed to the setQueue call that can handle this use case.

For example:

await musicKit.setQueue({ album: '1025210938', startTime: 10 });
...
await musicKit.play();

If you wanted to start playback immediately after setting the queue, you can also use the startPlaying: true option, which would negate the need for the separate play() call.

Accepted Answer

Hello, @gonzosan, and thank you for the question!

seekToTime() does indeed take seconds, as a Number, as the argument. This method requires there to be a nowPlayingItem, which is set when playback starts, which is likely why you are seeing an error when you attempt to seek before calling play().

Chaining the Promise is expected to behave the same as the await syntax you’re using above, so it would also make sense for that to not impact the behavior.

In MusicKit on the Web v1, which is the version documented in the link from your post, there is not a better way to seek to a specific time before playback starts.

In MusicKit on the Web v3, there is a startTime option in the Object passed to the setQueue call that can handle this use case.

For example:

await musicKit.setQueue({ album: '1025210938', startTime: 10 });
...
await musicKit.play();

If you wanted to start playback immediately after setting the queue, you can also use the startPlaying: true option, which would negate the need for the separate play() call.

Thank you, I'll add that in.

Music Kit JS SeekToTime
 
 
Q