Skip to content
Advertisement

Time and events in soundcloud embed

How can I get the duration of a track on SoundCloud in JavaScript or PHP? Can I use this duration with JavaScript events?

Advertisement

Answer

Working JSFiddle: https://jsfiddle.net/aj3Pw/1/

HTML:

<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F7659975" id="player"></iframe>

<button id="play">Play</button>

JS:

var iframe = document.querySelector('#player');
var sc = SC.Widget(iframe);

sc.bind(SC.Widget.Events.READY, function() {
    var d = document.createTextNode('Ready');
    document.querySelector('body').appendChild(d);
});

document.querySelector('button').addEventListener('click', function() {
    sc.play();
    sc.getDuration(function(duration) {
            console.log(duration);
            // do whatever you need to with the duration variable
        });
});

Relevant documentation: https://developers.soundcloud.com/docs/widget#methods

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement