Javascript HLS.js在使用主播放列表时一次加载所有子片段
在尝试为流式音频实现HLS.js时,我遇到了一个奇怪的问题,即即使音频根本没有播放,也会同时加载子播放列表的所有片段。如果我直接加载子播放列表,则其工作正常,即加载前两个片段并等待播放头,如果没有播放音频,则不加载其他片段。 同时加载的所有片段仅保留在主播放列表中Javascript HLS.js在使用主播放列表时一次加载所有子片段,javascript,http-live-streaming,hls.js,Javascript,Http Live Streaming,Hls.js,在尝试为流式音频实现HLS.js时,我遇到了一个奇怪的问题,即即使音频根本没有播放,也会同时加载子播放列表的所有片段。如果我直接加载子播放列表,则其工作正常,即加载前两个片段并等待播放头,如果没有播放音频,则不加载其他片段。 同时加载的所有片段仅保留在主播放列表中 <!-- Or if you want a more recent canary version --> <!-- <script src="https://cdn.jsdelivr.net/npm/hls.j
<!-- Or if you want a more recent canary version -->
<!-- <script src="https://cdn.jsdelivr.net/npm/hls.js@canary"></script> -->
<audio id="audio" controls></audio>
<script>
var audio = document.getElementById('audio');
if(Hls.isSupported()) {
var hls = new Hls();
hls.loadSource('https://neplay-test.s3.amazonaws.com/hls/master.m3u8');
hls.attachMedia(audio);
hls.on(Hls.Events.MANIFEST_PARSED,function() {
// audio.play();
});
}
// hls.js is not supported on platforms that do not have Media Source Extensions (MSE) enabled.
// When the browser has built-in HLS support (check using `canPlayType`), we can provide an HLS manifest (i.e. .m3u8 URL) directly to the video element through the `src` property.
// This is using the built-in support of the plain video element, without using hls.js.
// Note: it would be more normal to wait on the 'canplay' event below however on Safari (where you are most likely to find built-in HLS support) the video.src URL must be on the user-driven
// white-list before a 'canplay' event will be emitted; the last video event that can be reliably listened-for when the URL is not on the white-list is 'loadedmetadata'.
else if (audio.canPlayType('application/vnd.apple.mpegurl')) {
audio.src = 'https://neplay-test.s3.amazonaws.com/hls/master.m3u8';
audio.addEventListener('loadedmetadata',function() {
// audio.play();
});
}
</script>
屏幕截图:没有主播放列表,只有子播放列表
我只想在需要时加载片段,作为主播放列表的默认行为。我之前有你的问题,但我发现这不是hls.js的问题,
js默认缓冲区大小为60m,因此如果您的子列表文件小于60m,它将加载所有文件,您可以设置较小的缓冲区大小以节省带宽
/*假设设置为10米*/
新Hls(maxBufferSize:10*1000*1000)