Howler.js: Stop if already playing

Created on 3 Jan 2017  路  2Comments  路  Source: goldfire/howler.js

How can I stop whats currently playing and start playing a new file?

This is how i play sounds, if i call this function again while another sound is playing, i want it to stop and play the new sound.

  function playSound(audio) {

    var sound = new Howl({
      src: ['assets/sound/voice/' + audio]       
    });

    window.setTimeout(function(){
        sound.play();
     }
    }, 400);       

  }

Most helpful comment

This is how i do it by create global variable for 'sound' instead local variable.

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

By the way, why did you use setTimeout?

All 2 comments

This is how i do it by create global variable for 'sound' instead local variable.

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

By the way, why did you use setTimeout?

@ikhsanNCC that worked, thanks a lot :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bflora2 picture bflora2  路  4Comments

Friksel picture Friksel  路  4Comments

SandMoshi picture SandMoshi  路  3Comments

st-h picture st-h  路  5Comments

inglesuniversal picture inglesuniversal  路  4Comments