Stop audio buffering in the <audio> tag

痞子三分冷 提交于 2021-01-21 06:27:22

问题


I am currently working in using the HTML5 audio player to provide a audio stream (24/7 radio stream) via the (mobile) browser. Loading in the stream and playing it works fine.

The major problem is that the HTML5 <audio> tag will keep downloading (buffering) content even when its not active. This could be a major issue for mobile users since most of them pay for data use. So far I have not been able to find a decent solutions that works cross browser to prevent this.

I tried so far:

  1. Unload the source when pause is pressed. < This does not work cross browser
  2. Remove the audio player element and load a new one. This works but lets be honest, this is a very hacky way of performing an extremely simple task.

I was simply wondering if there is something I'm overlooking in this whole issue since I am convinced I'm not the only one with this issue.


回答1:


The <audio> element includes a preload attribute. This can be set to "none" or "metadata" which should prevent the audio preloading.

Source: https://developer.mozilla.org/en/docs/Web/HTML/Element/audio




回答2:


I found a workable solution for the problem described above. A detail description can be found here: https://stackoverflow.com/a/13302599/1580615




回答3:


You can do the following to stop buffering load without errors:

var blob = new Blob([], {type: "audio/mp3"});
var url = URL.createObjectURL(blob);
audio.src = _url;

or, shortened up:

audio.src = URL.createObjectURL(new Blob([], {type:"audio/mp3"});

Now you're not loading a "" which is a bad url for the audio tag to try and load. You're instead loading an actual url made from a Blob that just contains no data for the audio to playback.



来源:https://stackoverflow.com/questions/13242877/stop-audio-buffering-in-the-audio-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!