Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:Soundmanager2和Soundcloud事件回调_Javascript_Api_Soundcloud_Soundmanager2 - Fatal编程技术网

Javascript:Soundmanager2和Soundcloud事件回调

Javascript:Soundmanager2和Soundcloud事件回调,javascript,api,soundcloud,soundmanager2,Javascript,Api,Soundcloud,Soundmanager2,我正在尝试使用SoundManager2和soundcloud提供的选项构建自己的soundcloud媒体播放器 这是我目前的代码: SC.stream('/tracks/' + this.media.mediaId, function(audio){ audio.load({ onload : function(){ var duration = this.duration;

我正在尝试使用SoundManager2和soundcloud提供的选项构建自己的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();
音频播放将停止,所有回调将相应地触发。但是在“onfinish”回调之后,当我再次点击play时,它将重放音频,但不会触发任何事件


我做错了什么?

根据文档,“options”对象可以作为第二个参数传入:

SC.stream(轨迹路径、[options]、[callback])

这些选项将与
soundObject
本身相关联,因此在调用诸如
.load()
.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
,无论如何都能解决您眼前的问题。祝你好运

需要明确的是,@menzo wijmenga似乎正在使用相对常见的
var self=this
设计模式。在通过事件绑定访问音频选项的
duration
position
属性时,控制器可能具有类似
updatePlayButton
updateTimeLeft
的方法(即
self
指调用流的控制器,而
this
指音频对象)。