2013-08-28 41 views
5

我試圖使用SoundManager2和Soundcloud提供的選項來構建我自己的soundcloud媒體播放器。Javascript:Soundmanager2和Soundcloud事件回調

這裏是我當前的代碼:

SC.stream('/tracks/' + this.media.mediaId, function(audio){ 
      audio.load({ 
       onload : function(){ 
        var duration = this.duration; 

       }, 
       onfinish : function(){ 
        self.updatePlayButton(); 
        console.log('Finished'); 
       }, 
       onresume : function(){ 
        self.updatePlayButton(); 
        console.log("resumed"); 
       }, 
       onstop : function(){ 
        self.updatePlayButton(); 
        console.log("Stopped"); 
       }, 
       onpause : function() { 
        self.updatePlayButton(); 
        console.log('Paused'); 
       }, 
       whileplaying : function() 
       { 
        console.log(this.position); 
        self.updateScrubber(this.position/(this.duration/100)); 
        self.$timeLeft.text(self.formatTime(this.position/1000)); 
        console.log(totalPercent,'My position'); 
       } 

      }); 
      self.audio = audio.sID; 
      self.registerEvents(); 
     }); 

我玩使用音頻:

soundManager.getSoundById(self.audio).togglePause(); 

音頻播放的,而且所有的回調相應的解僱。但在「完成」回調之後,當我再次播放時,它會重播音頻,但不會觸發任何事件。

我做錯了什麼?

回答

4

根據對SC.stream()文檔中的「選項」對象可以被傳遞的第二個參數:

SC.stream(trackPath, [options], [callback])

的選項將與soundObject本身有關,所以你不會需要調用單獨的方法,如​​和.play()

嘗試使這樣的呼叫時再次聲明他們:

var myOptions = { 
    onload : function() { 
    var duration = this.duration; 
    }, 
    onfinish : function(){ 
    self.updatePlayButton(); 
    console.log('Finished'); 
    }, 
    onresume : function(){ 
    self.updatePlayButton(); 
    console.log("resumed"); 
    }, 
    onstop : function(){ 
    self.updatePlayButton(); 
    console.log("Stopped"); 
    }, 
    onpause : function() { 
    self.updatePlayButton(); 
    console.log('Paused'); 
    }, 
    whileplaying : function() { 
    console.log(this.position); 
    self.updateScrubber(this.position/(this.duration/100)); 
    self.$timeLeft.text(self.formatTime(this.position/1000)); 
    console.log(totalPercent,'My position'); 
    } 
} 

SC.stream('/tracks/' + this.media.mediaId, myOptions, function(audio){ 
    audio.load(); 
    self.audio = audio.sID; 
    self.registerEvents(); 
}); 

大約有你的代碼中的一些其他奇怪的東西,如不與onload函數內部的變量duration做任何事情。另外,使用變量self,同時也使用this最終看起來像你不確定你是否正在編寫Python或JavaScript,但也許這對你有意義。

希望以這種方式附加選項到soundObject將無論如何將解決您的直接問題。祝你好運!

+1

爲了清楚起見,它看起來像@ menzo-wijmenga正在使用相對常見的'var self = this'設計模式。當通過事件綁定訪問音頻選項的「duration」或「position」屬性時,控制器可能有類似'updatePlayButton'和'updateTimeLeft'的方法(即'self'指控制器正在調用流,而'this'指音頻對象)。 – elreimundo