如何在函数范围外与buzz.sound函数交互?

时间:2017-01-25 01:24:09

标签: javascript jquery

function audio(path2audio){
    var mySound = new buzz.sound ( path2audio, {
        formats: ["mp3"],
        preload: false,
        autoplay: false,
        loop: false,
        volume: 70,
    });
    mySound.play();
}

http://buzz.jaysalvat.com/documentation/sound/


Well, I need to make a trigger called outside this function in order to stop the audio file to stop playing. My difficulty is that whenever I call the method outside the function, it shows an error: "main.js:40 Uncaught TypeError: Cannot read property 'bind' of undefined".

mySound.bind('playing', function () {
    mySound.stop();
});

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

尝试将其分配给全局或外部范围中的变量。

var myRealSound;
function audio(path2audio){
    var mySound = new buzz.sound ( path2audio, {
        formats: ["mp3"],
        preload: false,
        autoplay: false,
        loop: false,
        volume: 70,
    });

    myRealSound = mySound;

    myRealSound.play();
}


myRealSound.bind('playing', function () {
    myRealSound.stop();
});

OR ...在与函数相同的范围内进行事件绑定。