How to abort downloading an audio stream

I want to stop/abort audio instead of pause, how would this be done?

I was trying to figure out how to do that.

How to abort an audio stream instead of pausing it.

How would I be able to do that?

https://jsfiddle.net/s0ae7ph9/

(function iife() {
  "use strict";

  function getButtonContainer(el) {
    while (el.classList.contains("playButton") === false) {
      el = el.parentNode;
    }
    return el;
  }

  function getPlay(button) {
    return button;
  }

  function showPlayButton(button) {
    button.classList.remove("active");
  }

  function isPlaying(button) {
    const play = getPlay(button);
    return play.classList.contains("active");
  }

  function pauseAllButtons() {
    var buttons = document.querySelectorAll(".playButton");
    buttons.forEach(function hidePause(buttons) {
      if (isPlaying(buttons)) {
        showPlayButton(buttons);
      }
    });
  }

  function showPauseButton(button) {
    pauseAllButtons();
    button.classList.add("active");
  }

  function getAudio() {
    return document.querySelector("audio");
  }

  function playAudio(player, src) {
    player.volume = 1.0;
    if (player.getAttribute("src") !== src) {
      player.setAttribute("src", src);
    }
    player.play();
  }

  function showButton(button, opts) {
    if (opts.playing) {
      showPlayButton(button);
    } else {
      showPauseButton(button);
    }
  }

  function pauseAudio(player) {
    player.pause();
  }

  function manageAudio(player, opts) {
    if (opts.playing) {
      pauseAudio(player);
    } else {
      playAudio(player, opts.src);
    }
  }

  function playButton(button) {
    const player = getAudio();
    const playing = isPlaying(button);
    showButton(button, {
      playing
    });
    manageAudio(player, {
      playing,
      src: button.getAttribute("data-audio")
    });
  }

  function playButtonClickHandler(evt) {
    const button = getButtonContainer(evt.target);
    playButton(button);
  }

  const playButtons = document.querySelectorAll(".button");
  playButtons.forEach(function addHandler(el) {
    el.addEventListener("click", playButtonClickHandler);
  });
}());

What exactly is it you want to achieve, why is pausing a problem?

Anyway, I think just setting the src attribute on the audio element to an empty string would be good enough. You can also remove the element from the DOM after that as well.

https://html.spec.whatwg.org/multipage/media.html#best-practices-for-authors-using-media-elements

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.