0

I'm having trouble with this javascript function

function playaudio(p){                                                          
    sound.pause();
    $("#audiovoix")[0].currentTime = 0;
    $("#audiovoix").attr("src", "fiches_visite/"+id+"/audio/"+p+"_FR.mp3");
    console.log("fiches_visite/"+id+"/audio/"+p+"_FR.mp3");
    sound.load();
    sound.play();
    sound.onerror = function() {
        console.log("fichier introuvable");
        itvl = setTimeout(function() { 
        $('#slider_suivant').trigger('click'); }, tempo);
    };                                      
}

This function is called within a picture slider, and p is an identifier int. It changes the audio tag src, plays it, or trigger a click if an error is returned (i don't use ajax to check if file exists on purpose).

Problem is, if the file does not actually exists, it fetches another file in the folder and plays it. It should just return an error but it does not... Why an other file i didn't ask for is played ? How to avoid this ?

My audio folder is something like : 1_FR.mp3, 3_FR.mp3, etc.

Thanks

1 Answers1

0

Seems to be a cache problem, i finally found the solution on this thread HTML5 Audio. Replacing Element still the same audio file (cached) how to solve?

In my code i just added an argument after the audio filename and it solved the problem

$("#audiovoix").attr("src", "fiches_visite/"+id+"/audio/"+p+"_FR.mp3?cache-buster=" + new Date().getTime());
Community
  • 1
  • 1