How can I add multiple sources to an HTML5 audio tag, programmatically?

前端 未结 2 1071
清歌不尽
清歌不尽 2020-11-29 05:43

A lot of examples demonstrate multiple source tags nested in the audio tag, as a method to overcome codec compatibility across different browsers. Something like this -

2条回答
  •  情深已故
    2020-11-29 06:16

    You can use the same DOM methods as with any other element:

    var source= document.createElement('source');
    source.type= 'audio/ogg';
    source.src= 'audio/song.ogg';
    audio.appendChild(source);
    source= document.createElement('source');
    source.type= 'audio/mpeg';
    source.src= 'audio/song.mp3';
    audio.appendChild(source);
    

提交回复
热议问题