2016-12-28 82 views
0

我需要一些幫助實現.playing()方法,當我的playSound函數被調用時,我想停止當前正在播放的任何內容,並播放新請求的聲音,這裏是我的示例,不是它的每一個所謂的時間停止:如果已經播放,請停止

function playSound(audio) { 
     var playing; 
     var sound = new Howl({ 
     src: ['assets/sound/voice/' + audio], 
     autoplay: false, 
     volume: 0.9, 
     onplay: function() { 
      playing = true; 
     }, 
     onend: function() { 
      playing = false; 
     }   
     }); 

     if(playing) { 
     sound.stop(); 
     } else {    
     sound.play();  
     } 
    } 

回答

0

的解決方案,最終,是創建「聲音」的全局變量:

var sound = null; 
function playSound(audio) { 
    //check if sound is null, if not stop previous sound and unload it 
    if (sound != null) { 
     sound.stop(); 
     sound.unload(); 
     sound = null; 
    } 
    sound = new Howl({ 
     src: ['assets/sound/voice/' + audio] 
    }); 
    sound.play(); 
} 
相關問題