简体   繁体   English

Javascript HTML5 <audio> 多源

[英]Javascript HTML5 <audio> multiple source

I am trying to insert the same song into 2 different sources on my audio player using javascripts 'getElementByID'. 我正在尝试使用javascript'getElementByID'将同一首歌曲插入音频播放器的2个不同来源中。

HTML: HTML:

<audio id="audio">
   <source id="ogg" src="song1.ogg" type="audio/ogg">
   <source id="mp3" src="song2.mp3" type="audio/mp3">
   Your browser does not support the audio tag.
</audio>

JavaScript: JavaScript:

function songOne(){ 
    document.getElementById('ogg').src="../audio/pt/lllg/Panda's Thumb.ogg";
    document.getElementById('mp3').src="../audio/pt/lllg/Panda's Thumb.mp3";
    document.getElementById('songName').innerHTML="Panda's Thumb";
    audio.play();
}

As you can see, when the function runs, it places the .ogg file into the id="ogg" and the .mp3 into the id=".mp3". 如您所见,函数运行时,它将.ogg文件放入id =“ ogg”,并将.mp3放入id =“。mp3”。 Though, when I inspect the element the source has changed but the song will not play. 不过,当我检查元素时,来源已更改,但歌曲不会播放。

I can get it to work with only one source: 我可以将其仅与一个来源一起使用:

<audio id="audio" src=""></audio>

But then it will only play on browsers that support mp3 or ogg and the others miss out. 但是,它只能在支持mp3或ogg的浏览器上播放,而其他浏览器则会错过。 What am i doing wrong? 我究竟做错了什么?

I don't know much about HTML5 Audio, but try this: 我对HTML5音频了解不多,但是请尝试以下操作:

After you change the src attr, issue a audio.load(); 更改src属性后,发出audio.load(); command 命令

Check this article: http://html5doctor.com/native-audio-in-the-browser/ 检查本文: http : //html5doctor.com/native-audio-in-the-browser/

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

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