2014-09-29 24 views
2

我需要在howler.js中播放一些聲音,事情是我不知道如何鏈接它。如何鏈接howler.js上的聲音

例如,在串BCG

需要發揮b.ogg然後c.ogg最後g.ogg

如果我只是(後裝)用途:

sound.play('b'); 
sound.play('c'); 
sound.play('g'); 

所有這些都開始和重疊,這不是我所需要的。

我看到有一個唯一的屬性,但無法弄清楚如何正確使用它。

問候。

回答

4

您可以創建一個函數playString(yourString)來讀取每個字符並動態設置聲音的onend屬性。下面的例子應該發揮BCGAC

var sound = new Howl({ 
 
    urls: ['http://shrt.tf/abcdefg.mp3'], 
 
    volume: 1, 
 
    sprite: { 
 
     a: [0, 600], 
 
     b: [700, 500], 
 
     c: [1200, 600], 
 
     d: [1900, 500], 
 
     e: [2400, 500], 
 
     f: [2900, 500], 
 
     g: [3400, 500], 
 
    } 
 
}); 
 

 
Howl.prototype.playString = function(str){ 
 
    if(str.length>1){ 
 
     this._onend[0] = function(){this.playString(str.substring(1,str.length));}; 
 
    } else { 
 
     this._onend[0] = function(){}; 
 
    } 
 
    if(str.length>0){ 
 
     this.play(str.substring(0,1)); 
 
    } 
 
}; 
 

 
sound.playString('bcgac');
<script src="http://shrt.tf/howler.js"></script>

請注意,您也可以調整這個功能,當一個字符是不是在精靈的工作,或者使用名稱的數組,而不是一個字符串。

+0

非常感謝您,嘗試弄清楚這一點真的很糟糕。 – 2014-09-29 19:52:06

+0

blex,你在哪裏託管你的文件,給你的鏈接http://shrt.tf/abcdefg.mp3 我一直在尋找類似的東西,以便我可以測試聲音的JavaScript編程。 – 2015-09-11 02:32:42

+0

嗨@ChristopherGaston是我個人的測試服務器。如果你不想購買一個,你可以很容易地在你的電腦上安裝一個本地服務器,使用WAMP for Windows或MAMP for Mac。然後,您將能夠通過「http:// localhost/your_files」訪問您的文件。 – blex 2015-09-11 06:29:24

3

您可以使用此代碼whic不需要精靈(jsfiddleGithub issue):

var playlist = function(e) { 
    // initialisation: 
     pCount = 0; 
     playlistUrls = [ 
     "https://upload.wikimedia.org/wikipedia/commons/8/8a/Zh-Beijing.ogg", 
     "https://upload.wikimedia.org/wikipedia/commons/8/8a/Zh-Beijing.ogg", 
     "./audio/a.mp3", 
     "./audio/b.mp3", 
     "./audio/c.mp3", 
     "./audio/d.mp3" 
     ], // audio list 
     howlerBank = [], 
     loop = true; 

    // playing i+1 audio (= chaining audio files) 
    var onEnd = function(e) { 
     if (loop === true) { pCount = (pCount + 1 !== howlerBank.length)? pCount + 1 : 0; } 
     else { pCount = pCount + 1; } 
     howlerBank[pCount].play(); 
    }; 

    // build up howlerBank:  
    playlistUrls.forEach(function(current, i) { 
     howlerBank.push(new Howl({ urls: [playlistUrls[i]], onend: onEnd, buffer: true })) 
    }); 

    // initiate the whole : 
     howlerBank[0].play(); 
} 

如果你做一個分享,請你回來的變化。

0

這是我的簡單解決方案,我使用的是小文件,所以下載延遲不是問題。聲音在這種情況下是全球性的

function play_audio(file_names) { 
    sound = new Howl({ 
     src: [audio_url+file_names[0]], 
     volume: 0.5, 
     onend: function() { 
      file_names.shift(); 
      if (file_names.length > 0) { 
       play_audio(file_names); 
      } 
     } 
    });  
    sound.play(); 
}