0

In a Spotify HTML5 app (version 1.0), how do I call a function every time the current position of the player changes (models.player.position)? I mean, like a setInterval millisecond function, but in sync with the player. I've tried "change:track" and "change", those fired perfectly when the track changes and when the player pauses (respectively).

models.player.addEventListener('change:track', updateCurrentTrack);
models.player.addEventListener('change', updateStatus);

Thanks in advance and sorry for my bad English!

EDIT: A function fired every second or 500 milliseconds will be perfect.

Thomas
  • 3,348
  • 4
  • 35
  • 49
josemmo
  • 6,523
  • 3
  • 35
  • 49
  • Do you mean like for every second a track change? – DannyThunder Nov 04 '13 at 08:33
  • Doesn't look like it. I was thinking you could use RealtimeAnalyzer or BufferAnalyzer in api/audio, but that seems like a lot of overhead and there is no interval control there. – Thomas Nov 04 '13 at 14:27
  • @Thomas, I've tried RealtimeAnalyzer and it seems to fire every 250 milliseconds, but I can't get the current position from it. – josemmo Nov 04 '13 at 15:10
  • Is models.player.position not accurate when it fires? – Thomas Nov 04 '13 at 15:15

1 Answers1

0

On my desktop, this prints every 850-950 ms.

require(['$api/audio', '$api/models'], function (audio, models) {
    var analyzer = audio.RealtimeAnalyzer.forPlayer(models.player);

    var last_position = -1;
    analyzer.addEventListener('audio', function (evt) {
        models.player.load('position').done(function() {
            if(models.player.position != last_position) { 
                console.log(models.player.position + ", " + (models.player.position-last_position)); 
                last_position = models.player.position;
            }
        });
    });
});

Not the best solution, for sure.

OTOH, the realtime analyzer is supposed to be reporting info pretty reliably with the music, I'd suspect you could just count calls and assume you're very close to multiples of 256 ms.

Thomas
  • 3,348
  • 4
  • 35
  • 49
  • Thanks! It's just what I wanted! It will not be the best solution, but it works. – josemmo Nov 04 '13 at 18:55
  • If you go with this, you can probably improve performance by adding something to skip every other call to models.player.load('position'). – Thomas Nov 04 '13 at 19:28