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:
JavaScript
x
4
1
<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>
2
3
<button id="play">Play</button>
4
JS:
JavaScript
1
16
16
1
var iframe = document.querySelector('#player');
2
var sc = SC.Widget(iframe);
3
4
sc.bind(SC.Widget.Events.READY, function() {
5
var d = document.createTextNode('Ready');
6
document.querySelector('body').appendChild(d);
7
});
8
9
document.querySelector('button').addEventListener('click', function() {
10
sc.play();
11
sc.getDuration(function(duration) {
12
console.log(duration);
13
// do whatever you need to with the duration variable
14
});
15
});
16
Relevant documentation: https://developers.soundcloud.com/docs/widget#methods