通过jQuery的改变源之后,HTML音频无法找到文件 [英] html audio cannot find file after changing source through jQuery
问题描述
我有这样的声音元素在我的.html文件:
<音频ID =验证码>
<信源ID =capt_wavSRC =doghouse.wavTYPE =音频/ WAV>
< /音频>
块引用>和它播放该音频。现在我需要使用jQuery动态地改变src属性。
然后我把原来的HTML音频没有源(SRC =)和我使用这个jQuery的code设置源:$('#capt_wav')ATTR('src'中,doghouse.wav);
块引用>这实际上改变的来源,因为我可以从控制台中看到:
在
<信源ID =capt_wavSRC =类型=音频/ WAV>
块引用>在
<信源ID =capt_wavSRC =doghouse.wavTYPE =音频/ WAV>
块引用>但在这种情况下,它不播放音频,给我的错误:
无效的URI。媒体资源的加载失败。 @文件:///home/index.html
所有候选资源加载失败。媒体负载暂停。 @文件:///home/index.html
块引用>文件夹中,任何想法,为什么它不工作?的HTML(其源)是在这两种情况下是相同的。
解决方案试试这个:
。$(#capt_wav),ATTR(SRC,doghouse.wav)分离()appendTo(#CAPTCHA);
看来浏览器只播放文件时,元素被添加,而不是当它只是修改
I have this audio element in my .html file:
<audio id ="captcha"> <source id = "capt_wav" src = "doghouse.wav" type = "audio/wav" > </audio>
and it plays the audio. Now I need to change the src attribute dynamically using jQuery. I then set the original html audio to have no source (src = "") and I use this jQuery code to set the source:
$('#capt_wav').attr('src',"doghouse.wav");
This actually changes the source, as I can see from the console:
BEFORE:
<source id="capt_wav" src="" type="audio/wav">
AFTER:
<source id="capt_wav" src="doghouse.wav" type="audio/wav">
but in this case it doesn't play the audio, giving me the errors:
Invalid URI. Load of media resource failed. @ file:///home/index.html
All candidate resources failed to load. Media load paused. @ file:///home/index.html
The file is in the folder, any idea why it doesn't work? The html (its source) is identical in both cases.
解决方案Try this instead:
$("#capt_wav").attr("src", "doghouse.wav").detach().appendTo("#captcha");
It seems the browser only plays the file when the element is added, not when it is just modified.
这篇关于通过jQuery的改变源之后,HTML音频无法找到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!