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();
});


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

最佳答案

尝试将其分配给全局或外部作用域中的变量。

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();
});


或者...在与您的函数相同的作用域内进行事件绑定。

10-05 21:06
查看更多