Nov
12
2023
JavaScript中动态加载音频的方法
作者:
绝缘体.. 发布:
2023-11-12 16:37 分类:
未分类 阅读:
抢沙发
使用Ajax方法从服务器获取音频资源并通过设置src属性加载音频。例如:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://www.phpernote.com/audio.mp3', true);
xhr.responseType = 'blob';
xhr.onload = function(){
if(this.status == 200){
var audio = new Audio(URL.createObjectURL(this.response));
audio.play();
}
};
xhr.send();
微信扫一扫,打赏作者吧~