How can I stop and resume a live audio stream in HTML5 instead of just pausing it?

后端 未结 3 1170
情书的邮戳
情书的邮戳 2021-01-18 03:42

A notable issue that\'s appearing as I\'m building a simple audio streaming element in HTML5 is that the tag doesn\'t behave as one would expect i

3条回答
  •  天涯浪人
    2021-01-18 03:56

    Resetting the audio source and calling the load() method seems to be the simplest solution when you want to stop downloading from the stream.

    Since it's a stream, the browser will stop downloading only when the user gets offline. Resetting is necessary to protect your users from burning through their cellular data or to avoid serving outdated content that the browser downloaded when they paused the audio.

    Keep in mind though that when the source attribute is set to an empty string, like so audio.src = "", the audio source will instead be set to the page's hostname. If you use a random word, that word will be appended as a path.

    So as seen below, setting audio.src ="", means that audio.src === "https://stacksnippets.net/js". Setting audio.src="meow" will make the source be audio.src === "https://stacksnippets.net/js/meow" instead. Thus the 3d paragraph is not visible.

    const audio1 = document.getElementById('audio1');
    const audio2 = document.getElementById('audio2');
    document.getElementById('p1').innerHTML = `First audio source: ${audio1.src}`;
    document.getElementById('p2').innerHTML = `Second audio source: ${audio2.src}`;
    
    if (audio1.src === "") {
      document.getElementById('p3').innerHTML = "You can see me because the audio source is set to an empty string";
    }
    
    
    
    

    Be aware of that behavior if you do rely on the audio's source at a given moment. Using the about URI scheme seems to trick it into behaving in a more reliable way. So using "about:" or "about:about", "about:blank", etc. will work fine.

    const resetAudioSource = "about:"
    
    const audio = document.getElementById('audio');
    audio.src = resetAudioSource;
    
    document.getElementById('p1').innerHTML = `Audio source: -- "${audio.src}"`;
    
    // Somewhere else in your code...
    
    if (audio.src === resetAudioSource){
      document.getElementById('p2').innerHTML = "You can see me because you reset the audio source."
    }
    
    

    Resetting the audio.src and calling the .load() method will make the audio to try to load the new source. The above comes in handy if you want to show a spinner component while the audio is loading, but don't want to also show that component when you reset your audio source.

    A working example can be found here: https://jsfiddle.net/v2xuczrq/

    If the source is reset using a random word, then you might end up with the loader showing up when you also pause the audio, or until the onError event handler catches it. https://jsfiddle.net/jcwvue0s/

    UPDATE: The strings "javascript:;" and "javascript:void(0)" can be used instead of the "about:" URI and this seems to work even better as it will also stop the console warnings caused by "about:".

提交回复
热议问题