如何在单击新按钮后中止/销毁先前的HTML5音频流?

huangapple go评论55阅读模式
英文:

How to Abort/destroy HTML5 audio stream from previous stream after new button is clicked?

问题

以下是您要翻译的代码部分:

const buttons = document.querySelectorAll('.playButton');
const audios = document.querySelectorAll('audio');

buttons.forEach((button, index) => {
  button.addEventListener('click', () => {
    audios.forEach((audio, audioIndex) => {
      if (index === audioIndex) {
        if (audio.paused) {
          audio.play();
        } else {
          audio.pause();
        }
      } else {
        audio.pause();
      }
    });
  });
});
html,
body {
  height: 100%;
  background: url("https://i.imgur.com/TSD1Xp1.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;
  background-position: center;
  padding: 0;
  margin: 0;
}

body {
  display: flex;
}

.playButtonContainer {
  display: flex;
  flex-wrap: wrap;
  min-height: 100%;
  margin: auto;
  justify-content: center;
  align-content: center;
  max-width: 800px;
  gap: 18px;
  animation: fadeInButtons 3s ease-in 0s forwards;
}

.buttonContainer {
  display: flex;
  flex-wrap: wrap;
  min-height: 100%;
  margin: auto;
  justify-content: center;
  align-content: center;
  max-width: 600px;
  gap: 18px;
  animation: fadeInButtons 3s ease-in 0s forwards;
}

audio {
  width: 200px;
}
<div class="playButtonContainer ">
  <audio title="Praise 95" preload="none">
    <source src="https://18093.live.streamtheworld.com/SP_R3935365_SC" type="audio/mpeg" />
  </audio>
  <audio title="Relax Club Music" preload="none">
    <source src="https://getradio.me/rcm" type="audio/mpeg" />
  </audio>
  <audio title="Sound Park #Deep" preload="none">
    <source src="https://getradio.me/spdeep" type="audio/mpeg" />
  </audio>

  <div class="buttonContainer">
    <button class="playButton">Praise 95</button>
    <button class="playButton">Relax Club Music</button>
    <button class="playButton">Sound Park #Deep</button>
  </div>
</div>
英文:

If you are watching tv, you change the channel, then you go back to the pervious channel, you aren't watching it from the beginning.

Is that more understandable to what I am trying to do?

Where the stream stops downloading in the background.

Goes back to as if the button was never clicked.

That is what I am trying to do in the code.

const buttons = document.querySelectorAll(&#39;.playButton&#39;);
const audios = document.querySelectorAll(&#39;audio&#39;);

buttons.forEach((button, index) =&gt; {
  button.addEventListener(&#39;click&#39;, () =&gt; {
    audios.forEach((audio, audioIndex) =&gt; {
      if (index === audioIndex) {
        if (audio.paused) {
          audio.play();
        } else {
          audio.pause();
        }
      } else {
        audio.pause();
      }
    });
  });
});

How would this be done?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const buttons = document.querySelectorAll(&#39;.playButton&#39;);
const audios = document.querySelectorAll(&#39;audio&#39;);

buttons.forEach((button, index) =&gt; {
  button.addEventListener(&#39;click&#39;, () =&gt; {
    audios.forEach((audio, audioIndex) =&gt; {
      if (index === audioIndex) {
        if (audio.paused) {
          audio.play();
        } else {
          audio.pause();
        }
      } else {
        audio.pause();
      }
    });
  });
});

<!-- language: lang-css -->

html,
body {
  height: 100%;
  background: url(&quot;https://i.imgur.com/TSD1Xp1.jpg&quot;);
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;
  background-position: center;
  padding: 0;
  margin: 0;
}

body {
  display: flex;
}

.playButtonContainer {
  display: flex;
  flex-wrap: wrap;
  min-height: 100%;
  margin: auto;
  justify-content: center;
  align-content: center;
  max-width: 800px;
  gap: 18px;
  animation: fadeInButtons 3s ease-in 0s forwards;
}

.buttonContainer {
  display: flex;
  flex-wrap: wrap;
  min-height: 100%;
  margin: auto;
  justify-content: center;
  align-content: center;
  max-width: 600px;
  gap: 18px;
  animation: fadeInButtons 3s ease-in 0s forwards;
}


audio {
  width: 200px;
}

<!-- language: lang-html -->

&lt;div class=&quot;playButtonContainer &quot;&gt;
  &lt;audio title=&quot;Praise 95&quot; preload=&quot;none&quot;&gt;
    &lt;source src=&quot;https://18093.live.streamtheworld.com/SP_R3935365_SC&quot; type=&quot;audio/mpeg&quot; /&gt;
  &lt;/audio&gt;
  &lt;audio title=&quot;Relax Club Music&quot; preload=&quot;none&quot;&gt;
    &lt;source src=&quot;https://getradio.me/rcm&quot; type=&quot;audio/mpeg&quot; /&gt;
  &lt;/audio&gt;
  &lt;audio title=&quot;Sound Park #Deep&quot; preload=&quot;none&quot;&gt;
    &lt;source src=&quot;https://getradio.me/spdeep&quot; type=&quot;audio/mpeg&quot; /&gt;
  &lt;/audio&gt;

  &lt;div class=&quot;buttonContainer&quot;&gt;
    &lt;button class=&quot;playButton&quot;&gt;Praise 95&lt;/button&gt;
    &lt;button class=&quot;playButton&quot;&gt;Relax Club Music&lt;/button&gt;
    &lt;button class=&quot;playButton&quot;&gt;Sound Park #Deep&lt;/button&gt;

  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 0

我能够弄清楚它。

const buttons = document.querySelectorAll('.playButton');
const audios = document.querySelectorAll('audio');

buttons.forEach((button, buttonIndex) => {
  button.addEventListener('click', () => {
    buttons.forEach((button) => button.classList.remove('blue'));
    audios.forEach((audio, audioIndex) => {
      audio.src = ""
      if (buttonIndex === audioIndex) {
        button.classList.add('blue')
        audio.src = button.dataset.src;
        audio.play();
      }
    });
  });
});
html,
body {
  height: 100%;
  background: url("https://i.imgur.com/TSD1Xp1.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;
  background-position: center;
  padding: 0;
  margin: 0;
}

body {
  display: flex;
}

.playButtonContainer {
  display: flex;
  flex-wrap: wrap;
  min-height: 100%;
  margin: auto;
  justify-content: center;
  align-content: center;
  max-width: 800px;
  gap: 18px;
  animation: fadeInButtons 3s ease-in 0s forwards;
}

.buttonContainer {
  display: flex;
  flex-wrap: wrap;
  min-height: 100%;
  margin: auto;
  justify-content: center;
  align-content: center;
  max-width: 600px;
  gap: 18px;
}

.playButton{
  cursor: pointer;
}

.playButton.blue {
  background-color: cornflowerblue;
  border: 2px solid purple;
}

audio {
  width: 200px;
}
<div class="playButtonContainer ">
  <audio title="Praise 95" preload="none">
    <source type="audio/mpeg" />
  </audio>

  <audio title="Relax Club Music" preload="none">
    <source type="audio/mpeg" />
  </audio>
  <audio title="Sound Park #Deep" preload="none">
    <source type="audio/mpeg" />
  </audio>
  <audio title="Sound Park Classic" preload="none">
    <source type="audio/mpeg" />
  </audio>
  <audio title="Delish Deep" preload="none">
    <source type="audio/mpeg" />
  </audio>
  <audio title="Music Factory Radio" preload="none">
    <source type="audio/mpeg" />
  </audio>
  <audio title="CBOE FM" preload="none">
    <source type="audio/mpeg" />
  </audio>
  <audio title="M.Deep Radio" preload="none">
    <source type="audio/mpeg" />
  </audio>
  <audio title="Fly 104" preload="none">
    <source type="audio/mpeg" />
  </audio>
  <audio title="Luxury Music" preload="none">
    <source type="audio/mpeg" />
  </audio>
  <audio title="Rock Melodic Radio" preload="none">
    <source type="audio/mpeg" />
  </audio>
  <audio title="Nu Euphoria" preload="none">
    <source type="audio/mpeg" />
  </audio>
  <audio title="Cavo Paradiso Club" preload="none">
    <source type="audio/mpeg" />
  </audio>
  <audio title="90s Eurodance" preload="none">
    <source type="audio/mpeg" />
  </audio>

  <div class="buttonContainer">
    <button class="playButton" data-src="https://18093.live.streamtheworld.com/SP_R3935365_SC">Praise 95</button>
    <button class="playButton" data-src="https://getradio.me/rcm">Relax Club Music</button>
    <button class="playButton" data-src="https://getradio.me/spdeep">Sound Park #Deep</button>
    <button class="playButton" data-src="https://getradio.me/spclassic">Sound Park Classic</button>
    <button class="playButton" data-src="https://getradio.me/delishdeep">Delish Deep</button>
    <button class="playButton" data-src="https://i4.streams.ovh/sc/musicfactory/stream">Music Factory Radio</button>
    <button class="playButton" data-src="https://getradio.me/svoefm">CBOE FM</button>
    <button class="playButton" data-src="https://deephousex.ru/live">M.Deep Radio</button>
    <button class="playButton" data-src="https://imagine2.radioca.st/;">Fly 104</button>
    <button class="playButton" data-src="https://getradio.me/luxurious.music">Luxury Music</button>
    <button class="playButton" data-src="https://i4.streams.ovh:2200/ssl/rockmelo?mp=/stream">Rock Melodic Radio</button>
    <button class="playButton" data-src="https://radio.nueuphoria.com:8443/live">Nu Euphoria</button>
    <button class="playButton" data-src="https://neos.win:48488/1">Cavo Paradiso Club</button>
    <button class="playButton" data-src="https://myradio24.org/5967">90s Eurodance</button>
  </div>
</div>
英文:

I was able to figure it out.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const buttons = document.querySelectorAll(&#39;.playButton&#39;);
const audios = document.querySelectorAll(&#39;audio&#39;);
buttons.forEach((button, buttonIndex) =&gt; {
button.addEventListener(&#39;click&#39;, () =&gt; {
buttons.forEach((button) =&gt; button.classList.remove(&#39;blue&#39;));
audios.forEach((audio, audioIndex) =&gt; {
audio.src = &quot;&quot;
if (buttonIndex === audioIndex) {
button.classList.add(&#39;blue&#39;)
audio.src = button.dataset.src;
audio.play();
}
});
});
});

<!-- language: lang-css -->

html,
body {
height: 100%;
background: url(&quot;https://i.imgur.com/TSD1Xp1.jpg&quot;);
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
background-position: center;
padding: 0;
margin: 0;
}
body {
display: flex;
}
.playButtonContainer {
display: flex;
flex-wrap: wrap;
min-height: 100%;
margin: auto;
justify-content: center;
align-content: center;
max-width: 800px;
gap: 18px;
animation: fadeInButtons 3s ease-in 0s forwards;
}
.buttonContainer {
display: flex;
flex-wrap: wrap;
min-height: 100%;
margin: auto;
justify-content: center;
align-content: center;
max-width: 600px;
gap: 18px;
}
.playButton{
cursor: pointer;
}
.playButton.blue {
background-color: cornflowerblue;
border: 2px solid purple;
}
audio {
width: 200px;
}

<!-- language: lang-html -->

&lt;div class=&quot;playButtonContainer &quot;&gt;
&lt;audio title=&quot;Praise 95&quot; preload=&quot;none&quot;&gt;
&lt;source type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;audio title=&quot;Relax Club Music&quot; preload=&quot;none&quot;&gt;
&lt;source type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;audio title=&quot;Sound Park #Deep&quot; preload=&quot;none&quot;&gt;
&lt;source type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;audio title=&quot;Sound Park Classic&quot; preload=&quot;none&quot;&gt;
&lt;source type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;audio title=&quot;Delish Deep&quot; preload=&quot;none&quot;&gt;
&lt;source type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;audio title=&quot;Music Factory Radio&quot; preload=&quot;none&quot;&gt;
&lt;source type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;audio title=&quot;CBOE FM&quot; preload=&quot;none&quot;&gt;
&lt;source type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;audio title=&quot;M.Deep Radio&quot; preload=&quot;none&quot;&gt;
&lt;source type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;audio title=&quot;Fly 104&quot; preload=&quot;none&quot;&gt;
&lt;source type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;audio title=&quot;Luxury Music&quot; preload=&quot;none&quot;&gt;
&lt;source type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;audio title=&quot;Rock Melodic Radio&quot; preload=&quot;none&quot;&gt;
&lt;source type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;audio title=&quot;Nu Euphoria&quot; preload=&quot;none&quot;&gt;
&lt;source type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;audio title=&quot;Cavo Paradiso Club&quot; preload=&quot;none&quot;&gt;
&lt;source type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;audio title=&quot;90s Eurodance&quot; preload=&quot;none&quot;&gt;
&lt;source type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;div class=&quot;buttonContainer&quot;&gt;
&lt;button class=&quot;playButton&quot; data-src=&quot;https://18093.live.streamtheworld.com/SP_R3935365_SC&quot;&gt;Praise 95&lt;/button&gt;
&lt;button class=&quot;playButton&quot; data-src=&quot;https://getradio.me/rcm&quot;&gt;Relax Club Music&lt;/button&gt;
&lt;button class=&quot;playButton&quot; data-src=&quot;https://getradio.me/spdeep&quot;&gt;Sound Park #Deep&lt;/button&gt;
&lt;button class=&quot;playButton&quot; data-src=&quot;https://getradio.me/spclassic&quot;&gt;Sound Park Classic&lt;/button&gt;
&lt;button class=&quot;playButton&quot; data-src=&quot;https://getradio.me/delishdeep&quot;&gt;Delish Deep&lt;/button&gt;
&lt;button class=&quot;playButton&quot; data-src=&quot;https://i4.streams.ovh/sc/musicfactory/stream&quot;&gt;Music Factory Radio&lt;/button&gt;
&lt;button class=&quot;playButton&quot; data-src=&quot;https://getradio.me/svoefm&quot;&gt;CBOE FM&lt;/button&gt;
&lt;button class=&quot;playButton&quot; data-src=&quot;https://deephousex.ru/live&quot;&gt;M.Deep Radio&lt;/button&gt;
&lt;button class=&quot;playButton&quot; data-src=&quot;https://imagine2.radioca.st/;&quot;&gt;Fly 104&lt;/button&gt;
&lt;button class=&quot;playButton&quot; data-src=&quot;https://getradio.me/luxurious.music&quot;&gt;Luxury Music&lt;/button&gt;
&lt;button class=&quot;playButton&quot; data-src=&quot;https://i4.streams.ovh:2200/ssl/rockmelo?mp=/stream&quot;&gt;Rock Melodic Radio&lt;/button&gt;
&lt;button class=&quot;playButton&quot; data-src=&quot;https://radio.nueuphoria.com:8443/live&quot;&gt;Nu Euphoria&lt;/button&gt;
&lt;button class=&quot;playButton&quot; data-src=&quot;https://neos.win:48488/1&quot;&gt;Cavo Paradiso Club&lt;/button&gt;
&lt;button class=&quot;playButton&quot; data-src=&quot;https://myradio24.org/5967&quot;&gt;90s Eurodance&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月19日 21:46:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定