如何强制浏览器只下载 audio/video 元素的一部分?

How to force the browser to download only part of an audio/video element?

我有一个 audio 元素,我通过 JavaScript 控制它:

<audio preload="metadata" src="myfile.mp3" />

<button type="button">Play/Pause</button>

// JS logic not relevant to the question

假设 myfile.mp3 是一个 5 分钟的音频,在播放前 10 秒后,我调用 pause() 以防止播放其余部分。浏览器似乎继续下载文件的其余部分。我怎样才能防止这种情况发生并只下载文件的特定块?

试试这个:

const mediaElement = document.querySelector("#myMediaElementID");
mediaElement.removeAttribute("src");
mediaElement.load();

By removing the media element's src attribute and invoking the load() method, you release the resources associated with the audio/video, which stops the network download. You must call load() after removing the attribute, because just removing the src attribute does not invoke the load algorithm.

https://developer.mozilla.org/en-US/docs/Web/Guide/Audio_and_video_delivery