0

I have been writing Spotify support for a project using libspotify, and when I wanted to implement seeking, I noticed that there is apparently no function to get the current playback position. In other words, a counterpart to sp_session_player_seek(), which returns the current offset.

What some people seem to do is to save the offset used in the last seek() call, and then accumulate the number of frames in a counter in music_delivery. Together with the stored offset, the current position can be calculated that way, yes - but is this really the only way how to do it?

I know that the Spotify Web API has a way to get the current position, so it is strange that libspotify doesn't have one.

dv_
  • 1,247
  • 10
  • 13

2 Answers2

1

Keeping track of it yourself the way to do it.

The way to look at it is this: libspotify doesn't actually play music, so it can't possibly know the current position.

All libspotify does it pass you PCM data. It's the application's job to get that out to the speakers, and audio pipelines invariably have various buffers and latencies and whatnot in place. When you seek, you're not saying "Move playback to here", you're saying "start giving me PCM from this point in the track".

In fact, if you're just counting frames and then passing them to an audio pipeline, your position is likely incorrect if it doesn't take into account that pipeline's buffers etc.

iKenndac
  • 18,730
  • 3
  • 35
  • 51
0

You can always track the position yourself.

For example:

 void SpSeek(int position)
 {
     sp_session_player_seek(mSession, position);
     mMsPosition = position;
 }


 int OnSessionMusicDelivery(sp_session *session, const sp_audioformat *format, const void *frames, int numFrames) 
 {
     return SendToAudioDriver(...)
 }

In my case i'm using OpenSL (Android), each time the buffer finish i update the value of the position like this

 mMsPosition += (frameCount * 1000) / UtPlayerGetSampleRate();

Where numFrames is the frames consumed by the driver.

Agustin Alvarez
  • 349
  • 1
  • 2
  • 11