且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何以编程方式将多个源添加到HTML5音频标签?

更新时间:2023-09-22 07:57:10

为什么只要检测支持的类型,为什么要用JavaScript添加多个文件?我建议改为检测***类型,然后设置 src

Why add multiple files with JavaScript when you can just detect the types supported? I would suggest instead detecting the best type then just setting the src.

var source= document.createElement('source');
if (audio.canPlayType('audio/mpeg;')) {
    source.type= 'audio/mpeg';
    source.src= 'audio/song.mp3';
} else {
    source.type= 'audio/ogg';
    source.src= 'audio/song.ogg';
}
audio.appendChild(source);

添加与文件类型一样多的支票。

Add as many checks as you have file types.