2017-01-25 30 views
0
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(); 
}); 

有人可以帮我解决这个问题吗?如何与功能范围外的buzz.sound函数进行交互?

回答

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

或...在与您的函数相同的范围内执行事件绑定。

+0

在我的情况下,我还不得不在jQuery的'$(document).ready(function(){')之外声明一个全局变量。 – Roger