onfinish on global soundmanager object

时间:2015-03-16 14:51:47

标签: javascript object soundmanager2

我想在声音结束时启动一个函数,这段代码有效:

var mysound;

var next = function(){
 alert('end');   
};

soundManager.onready(function () {

    mysound = soundManager.createSound({
        id:'foo',
        url:'http://soundbible.com/grab.php?id=2077&type=mp3'
    })
    mysound.play({onfinish:next});
    // change to this 
    // mysound.play();
 });
// in another .js
// mysound.onfinish(next) // doesn't work
// mysound.prototype.options.onfinish = next // doesn't work

我想做点评我做的事。

code on jsfiddle

1 个答案:

答案 0 :(得分:0)

问题是,mysound试图在它被设置为正确值之前分配onfinish。

var mysound;

var next = function(){
 alert('end');   
};
function importedFromOtherFile( mysound ) {
  mySound.onfinish(next);
}

soundManager.onready(function () {

    mysound = soundManager.createSound({
        id:'foo',
        url:'http://soundbible.com/grab.php?id=2077&type=mp3'
    })
    mysound.play({onfinish:next});
    // change to this 
    functionImportedFromOtherFile(mysound);
 });

如果你想让它在一个不同的文件中,看看你是否可以给它回电话,或者在它设置之后抛出一个偶数