Javascript HTML5 <audio> multiple source - Stack Overflow

I am trying to insert the same song into 2 different sources on my audio player using javascripts '

I am trying to insert the same song into 2 different sources on my audio player using javascripts 'getElementByID'.

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:

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". 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. What am i doing wrong?

I am trying to insert the same song into 2 different sources on my audio player using javascripts 'getElementByID'.

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:

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". 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. What am i doing wrong?

Share Improve this question edited Feb 19, 2014 at 10:36 BenMorel 36.7k52 gold badges206 silver badges337 bronze badges asked Oct 7, 2012 at 11:43 user1719526user1719526 731 silver badge6 bronze badges 2
  • 1 according to w3schools./html/html5_audio.asp your mp3 should have type="audio/mpeg" – xception Commented Oct 7, 2012 at 11:59
  • Thanks for the fix, I have changed this but it hasn't fixed it. – user1719526 Commented Oct 7, 2012 at 12:07
Add a ment  | 

1 Answer 1

Reset to default 5

I don't know much about HTML5 Audio, but try this:

After you change the src attr, issue a audio.load(); mand

Check this article: http://html5doctor./native-audio-in-the-browser/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744223757a4563908.html

相关推荐

  • Javascript HTML5 &lt;audio&gt; multiple source - Stack Overflow

    I am trying to insert the same song into 2 different sources on my audio player using javascripts '

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信