【问题标题】:How to abort downloading an audio stream如何中止下载音频流
【发布时间】:2021-11-12 18:04:29
【问题描述】:

我希望有人可以帮助解决这个问题。

我想停止/中止音频而不是暂停,该怎么做?

我试图弄清楚如何做到这一点。

这就是我想要弄清楚的全部内容。

如何中止音频流而不是暂停它。

我怎么能做到这一点?

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);
  });
}());
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

.outer {
  display: table;
  height: 100%;
  margin: 0 auto;
}

.tcell {
  display: table-cell;
  vertical-align: middle;
}

.wrap {
  position: relative;
  width: 190px;
  height: 235px;
  background: red;
}

.playButton {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  width: 150px;
  height: 195px;
  background-color: black;
  box-shadow: 0 0 10px 2px rgba(0, 0, 0, 0.2), 0 0 1px 2px black, inset 0 2px 2px -2px white, inset 0 0 2px 15px #47434c, inset 0 0 2px 22px black;
  border-radius: 5px;
  padding: 20px;
  perspective: 700px;
}

.playButton.active .button {
  transform: translateZ(20px) rotateX(25deg);
  box-shadow: 0 -10px 20px #ff1818;
}

.playButton.active .button .light {
  animation: flicker 0.2s infinite 0.3s;
}

.playButton.active .button .shine {
  opacity: 1;
}

.playButton.active .button .shadow {
  opacity: 0;
}

.playButton .button {
  transition: all 0.3s cubic-bezier(1, 0, 1, 1);
  transform-origin: center center -20px;
  transform: translateZ(20px) rotateX(-25deg);
  transform-style: preserve-3d;
  background-color: #9b0621;
  width: 100%;
  height: 100%;
  position: relative;
  cursor: pointer;
  background: linear-gradient(#980000 0%, #6f0000 30%, #6f0000 70%, #980000 100%);
  background-repeat: no-repeat;
}


.playButton .button::before {
  content: "";
  background: linear-gradient(rgba(255, 255, 255, 0.8) 10%, rgba(255, 255, 255, 0.3) 30%, #650000 75%, #320000) 50% 50%/97% 97%, #b10000;
  background-repeat: no-repeat;
  width: 100%;
  height: 50px;
  transform-origin: top;
  transform: rotateX(-90deg);
  position: absolute;
  top: 0;
}

.playButton .button::after {
  content: "";
  background-image: linear-gradient(#650000, #320000);
  width: 100%;
  height: 50px;
  transform-origin: top;
  transform: translateY(50px) rotateX(-90deg);
  position: absolute;
  bottom: 0;
  box-shadow: 0 50px 8px 0px black, 0 80px 20px 0px rgba(0, 0, 0, 0.5);
}

.playButton .light {
  opacity: 0;
  animation: light-off 1s;
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: radial-gradient(#ffc97e, #ff1818 40%, transparent 70%);
}

.playButton .dots {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: radial-gradient(transparent 30%, rgba(101, 0, 0, 0.7) 70%);
  background-size: 10px 10px;
}

.playButton .characters {
  position: absolute;
  width: 100%;
  height: 100%;
  background: linear-gradient(white, white) 50% 20%/5% 20%, radial-gradient(circle, transparent 50%, white 52%, white 70%, transparent 72%) 50% 80%/33% 25%;
  background-repeat: no-repeat;
}

.playButton .shine {
  transition: all 0.3s cubic-bezier(1, 0, 1, 1);
  opacity: 0.3;
  position: absolute;
  width: 100%;
  height: 100%;
  background: linear-gradient(white, transparent 3%) 50% 50%/97% 97%, linear-gradient(rgba(255, 255, 255, 0.5), transparent 50%, transparent 80%, rgba(255, 255, 255, 0.5)) 50% 50%/97% 97%;
  background-repeat: no-repeat;
}

.playButton .shadow {
  transition: all 0.3s cubic-bezier(1, 0, 1, 1);
  opacity: 1;
  position: absolute;
  width: 100%;
  height: 100%;
  background: linear-gradient(transparent 70%, rgba(0, 0, 0, 0.8));
  background-repeat: no-repeat;
}

@keyframes flicker {
  0% {
    opacity: 1;
  }

  80% {
    opacity: 0.8;
  }

  100% {
    opacity: 1;
  }
}

@keyframes light-off {
  0% {
    opacity: 1;
  }

  80% {
    opacity: 0;
  }
}
<audio></audio>
<div class="outer">
  <div class="tcell">
    <div class="wrap">
      <div class="playButton" data-audio="https://stream.sunfmua.com:8443/rock">
        <div class="button">
          <div class="light"></div>
          <div class="dots"></div>
          <div class="characters"></div>
          <div class="shine"></div>
          <div class="shadow"></div>
        </div>
      </div>
    </div>
  </div>
</div>

【问题讨论】:

  • 为什么要中止流而不是暂停,如果我知道您要达到的目标,我可以推荐另一种方法
  • 音频元素继承自 HTMLMediaElement。从之前的 SO 问题中,如果您想阻止 HTMLMediaElement 缓冲,那么答案是,您不能 stackoverflow.com/questions/29435360/…

标签: javascript audio html5-audio abort


【解决方案1】:

似乎HTML5 audio start over 之类的东西应该可以工作。我知道这适用于本地的音频文件,但不确定你的代码中的那个。我认为它应该工作相同。

【讨论】:

【解决方案2】:

如果你想最终停止加载媒体,你不能使用原生的HTMLMediaElement

您应该自己获取和流式传输媒体,并在您愿意时使用AbortController 中止它。 Aborting a fetch.

let controller;

// Play button event
playButton.addEventListener('click', playAudio);

// Stop button event
stopButton.addEventListener('click', () => {
    controller?.abort();
});

async function playAudio() {
  controler = new AbortController();

  const response = await fetch(mediaURL, { signal: controller.signal })
  const reader = await response.body.getReader();
  
  async function read() {
    const { value, done } =  await reader.read();
    /* value = audioChunk;
       Push the audio chunk to audio or video context
    */
  }
  
  read();
}

在流式传输响应时检查此example,或在流式传输音频时检查此function

【讨论】:

【解决方案3】:

我不确定,但请尝试

audio.currentTime = 0;
audio.play();

【讨论】:

    猜你喜欢
    • 2017-04-26
    • 2013-12-04
    • 2013-02-24
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 2014-08-25
    相关资源
    最近更新 更多