Javascript 设置音频src时如何发送标题?
我正在尝试使用html5音频标签播放音频文件 它演奏得很好 但出于某种原因,要求浏览器对源文件发出的请求必须包含浏览器(在我的例子中是safari)未添加的特定头文件。(如我所见,chrome添加了) 标题为:Javascript 设置音频src时如何发送标题?,javascript,html5-video,html5-audio,akamai,Javascript,Html5 Video,Html5 Audio,Akamai,我正在尝试使用html5音频标签播放音频文件 它演奏得很好 但出于某种原因,要求浏览器对源文件发出的请求必须包含浏览器(在我的例子中是safari)未添加的特定头文件。(如我所见,chrome添加了) 标题为:接受编码:gzip 我怎样才能做到这一点 我不想提前下载整个文件。。。我想音频标签来处理它,一个新的标题 document.getElementById(“音频”).load(); document.getElementById(“音频”).play() 既然您要做的是加载然后播放,那么
接受编码:gzip
我怎样才能做到这一点
我不想提前下载整个文件。。。我想音频标签来处理它,一个新的标题
document.getElementById(“音频”).load();
document.getElementById(“音频”).play()代码>
既然您要做的是加载然后播放,那么使用获取
之类的方式,将您的请求与XMLHttpRequest
样式模式结合起来怎么样
比如:
HTML
<!-- just an indicator, in case the file is large -->
<div id="loading"><em>File is loading. Please wait...</em></div>
<!-- initially hide the audio tag. Also, change the 'src' to 'data-src' so it doesn't automatically load -->
<audio id="audio" style="display: none;" data-src="https://upload.wikimedia.org/wikipedia/commons/transcoded/7/7b/FurElise.ogg/FurElise.ogg.mp3" controls></audio>
希望这能有所帮助。请注意,根据浏览器的不同,有时音频可能不会自动播放,除非用户首先与页面交互或明确给予许可。您尝试了什么?错误消息是什么?我尝试了什么?我在音频标签中添加了src,这就是我尝试的!我不知道如何设置音频标签的自定义标题,这是我问的,错误消息是什么?没有错误,问题是寻找一种方法来实现一些我不知道的事情,我读过一些人们说不是的答案possible@ceving我想你是糊涂了,我没有自己提出任何请求,当我调用load()时,浏览器正在这样做,我想截取它来修改它!问题需要回答。对于MCVE来说,它是相关的,它是有效的。否则它是不可复制的。@cerving好的,我会记住的,谢谢你编辑这个问题!我不想在播放之前下载整个文件。当您添加源并加载(比如chrome)时,浏览器会发出“范围请求”,请求媒体文件的后续部分,以便在某些数据可用时可以立即开始播放。但是,我使用的CDN要求这些请求具有此特定的标头,问题是在chrome中,这个标题是由chrome本身添加的,而在safari中它不是,因此我想拦截浏览器在调用媒体元素上的load()时发出的这些请求,并添加标题,希望您现在有一个更清晰的图片…好的-我看到您在原始问题中添加了这个澄清。
// define variables
var audioEl = document.getElementById("audio");
function fetchAudioFile() {
// go get the file
var requestObj = new Request(audioEl.dataset.src, {
method: 'GET',
headers: {
'Accept-encoding': 'gzip' // add your header(s)
},
referrerPolicy: 'no-referrer'
});
fetch(requestObj).then(function(response) {
return response;
}).then(async function(outcome) {
const blob = await outcome.blob();
// convert our blob to a url that can be used by the audio tag
const url = window.URL.createObjectURL(blob);
// replace the source
audioEl.src = url;
// hide the helpful text
document.getElementById("loading").style["display"] = "none";
// show the audio tag
audioEl.style["display"] = "block";
// play it immediately
audioEl.play();
});
};
// get the file
fetchAudioFile();