How to chain sounds on howler.js

故事扮演 提交于 2019-12-01 09:38:52

问题


I need to play some sounds in howler.js the thing is that I don't know how to chain it.

For example, at string BCG

would need to play b.ogg then c.ogg and finally g.ogg

If I just use (after loading):

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

All of them start and overlap which isn't what I need.

I see there's a onend property, however can't figure out how to use it properly.

Regards.


回答1:


You could create a function playString(yourString) that will read each character and dynamically set the onend property of your sound. The following example should play B C G A C:

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>

Note that you could also tweak this function to work when a character is not in the sprite, or to use an array of names instead of a string.




回答2:


You could use this code whic doesn't require sprites (jsfiddle, Github 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();
}

Please share back your variation if you do one.




回答3:


Here is my simple solution, I am using tiny files so download delay is not an issue. sound is global in this case

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


来源:https://stackoverflow.com/questions/26105483/how-to-chain-sounds-on-howler-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!