javascript - Soundcloud Widget - Event.PLAY not working -


i'm having problems soundcloud widget api.

for reason event.play not working anymore...

var i, song, iframes = document.getelementsbytagname('iframe');  (i = 0; < iframes.length; i++) {     if (string.prototype.indexof.call(iframes[i].src, '//w.soundcloud.com/player') > -1)      {                    iframes[i].id = "sc_player_"+i;         song = sc.widget(iframes[i].id);                             song.bind(sc.widget.events.play, function(eventdata){                            song.getcurrentsound(function(sound) {                 console.log(sound.title);             });         });     } } 

thanks reporting this, looking why play event doesn't fire on first play right now.

upd. bug play event not firing on initial play fixed now.

on side note, there's problem code: functions creating (event handlers) inside of loop reference last iframe song. should either wrap event handler creation in iffe passing current song paramater:

for (i = 0; < iframes.length; i++) {     if (src.indexof('//w.soundcloud.com/player') > -1) {                    iframes[i].id = "sc_player_"+i;         song = sc.widget(iframes[i].id);         song.bind(sc.widget.events.play, (function (song) {             return function(eventdata){                                song.getcurrentsound(function(sound) {                     console.log(sound.title);                 });             }         }(song)));     } } 

or create helper function return event handler referencing right song:

function createeventhandler (song) {   // `song` “caught” in closure   return function (eventdata) {     song.getcurrentsound(function (sound) {       console.log(sound.title);     });   } }  (i = 0; < iframes.length; i++) {     if (src.indexof('//w.soundcloud.com/player') > -1) {                    iframes[i].id = "sc_player_"+i;         song = sc.widget(iframes[i].id);         song.bind(sc.widget.events.play, createeventhandler(song));     } } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -