Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top