简体   繁体   English

Javascript函数播放错误的音频文件

[英]Javascript function plays wrong audio file

I'm having trouble with this javascript function 我在使用此javascript函数时遇到问题

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. 在图片滑块中调用此函数,并且p是标识符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). 如果返回错误,它将更改音频标签src,播放它或触发单击(我不使用ajax来检查文件是否故意存在)。

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. 我的音频文件夹类似于:1_FR.mp3,3_FR.mp3等。

Thanks 谢谢

Seems to be a cache problem, i finally found the solution on this thread HTML5 Audio. 似乎是缓存问题,我终于在此线程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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM