动态音频小部件播放相同的音频

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

Dynamic Audio Widgets playing same audio

问题

这是我的网页的简化版本 - 所有按钮都播放数组中的第一个元素,我不知道为什么。我怀疑这与forEach循环的执行方式有关。

// Listen Page
audioItems = [
  'http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/alien_hit.wav',
  'http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/alien_shoot.wav',
  'http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/bonus.wav',
]

const listenContainer = document.querySelector('.audio-widget-container');

audioItems.forEach(track => {
  newTrackWidget = document.createElement('div');
  newTrackWidget.classList.add('track-container')

  trackTitle = document.createElement("h3");
  trackTitle.textContent = track;

  trackButton = document.createElement("button");
  trackButton.innerHTML = `<i class="fa-solid fa-play"></i>`
  trackTimeline = document.createElement("div");

  trackAudio = document.createElement("audio");
  trackAudio.src = track;

  trackButton.addEventListener("click", () => {
    trackAudio.play()
  })

  newTrackWidget.appendChild(trackAudio)
  newTrackWidget.appendChild(trackTitle);
  newTrackWidget.appendChild(trackButton);
  newTrackWidget.appendChild(trackTimeline);

  listenContainer.appendChild(newTrackWidget);
});
<script src="https://kit.fontawesome.com/00c3160031.js" crossorigin="anonymous"></script>

<section class="audio-widget-container">
</section>
英文:

Here is a simplified version of my web page - all buttons are playing the first element in the array and I don't know why. I suspect it's something to do with the forEach loop not functioning in the correct way

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

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

// Listen Page
audioItems = [
&#39;http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/alien_hit.wav&#39;,
&#39;http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/alien_shoot.wav&#39;,
&#39;http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/bonus.wav&#39;,
]

const listenContainer = document.querySelector(&#39;.audio-widget-container&#39;);

audioItems.forEach(track =&gt; {
	newTrackWidget = document.createElement(&#39;div&#39;);
	newTrackWidget.classList.add(&#39;track-container&#39;)

	trackTitle = document.createElement(&quot;h3&quot;);
	trackTitle.textContent = track;

	trackButton = document.createElement(&quot;button&quot;);
	trackButton.innerHTML = `&lt;i class=&quot;fa-solid fa-play&quot;&gt;&lt;/i&gt;`
	trackTimeline = document.createElement(&quot;div&quot;);

	trackAudio = document.createElement(&quot;audio&quot;);
	trackAudio.src = track;

	trackButton.addEventListener(&quot;click&quot;, () =&gt; {
		trackAudio.play()
	})

	newTrackWidget.appendChild(trackAudio)
	newTrackWidget.appendChild(trackTitle);
	newTrackWidget.appendChild(trackButton);
	newTrackWidget.appendChild(trackTimeline);

	listenContainer.appendChild(newTrackWidget);
});

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

&lt;script src=&quot;https://kit.fontawesome.com/00c3160031.js&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

&lt;section class=&quot;audio-widget-container&quot;&gt;
&lt;/section&gt;

<!-- end snippet -->

答案1

得分: 1

你在没有关键字的情况下分配了变量,这意味着它们被提升到全局范围。因此声音被覆盖。要修复它,请在forEach循环内使用关键字let创建新变量,使它们唯一:

const audioItems = [
    'http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/alien_hit.wav',
    'http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/alien_shoot.wav',
    'http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/bonus.wav',
];

const listenContainer = document.querySelector('.audio-widget-container');

audioItems.forEach(track => {
    let newTrackWidget = document.createElement('div');
    newTrackWidget.classList.add('track-container');

    let trackTitle = document.createElement('h3');
    trackTitle.textContent = track;

    let trackButton = document.createElement('button');
    trackButton.innerHTML = `<i class="fa-solid fa-play"></i>`;
    let trackTimeline = document.createElement('div');
    let trackAudio = document.createElement('audio');
    trackAudio.src = track;

    trackButton.addEventListener('click', () => {
        trackAudio.play();
    });

    newTrackWidget.appendChild(trackAudio);
    newTrackWidget.appendChild(trackTitle);
    newTrackWidget.appendChild(trackButton);
    newTrackWidget.appendChild(trackTimeline);

    listenContainer.appendChild(newTrackWidget);
});
<script src="https://kit.fontawesome.com/00c3160031.js" crossorigin="anonymous"></script>

<section class="audio-widget-container">
</section>
英文:

You assigned to variables without keywords, which mean that they are hoisted to global scope.Therefore the sound was overwritten. To fix it create new variables inside forEach loop using keyword let to make them unique:

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

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

const audioItems = [
    &#39;http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/alien_hit.wav&#39;,
    &#39;http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/alien_shoot.wav&#39;,
    &#39;http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/bonus.wav&#39;,
];

const listenContainer = document.querySelector(&#39;.audio-widget-container&#39;);

audioItems.forEach(track =&gt; {
    let newTrackWidget = document.createElement(&#39;div&#39;);
    newTrackWidget.classList.add(&#39;track-container&#39;);

    let trackTitle = document.createElement(&#39;h3&#39;);
    trackTitle.textContent = track;

    let trackButton = document.createElement(&#39;button&#39;);
    trackButton.innerHTML = `&lt;i class=&quot;fa-solid fa-play&quot;&gt;&lt;/i&gt;`;
    let trackTimeline = document.createElement(&#39;div&#39;);
    let trackAudio = document.createElement(&#39;audio&#39;);
    trackAudio.src = track;

    trackButton.addEventListener(&#39;click&#39;, () =&gt; {
        trackAudio.play();
    });

    newTrackWidget.appendChild(trackAudio);
    newTrackWidget.appendChild(trackTitle);
    newTrackWidget.appendChild(trackButton);
    newTrackWidget.appendChild(trackTimeline);

    listenContainer.appendChild(newTrackWidget);
});

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

&lt;script src=&quot;https://kit.fontawesome.com/00c3160031.js&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

&lt;section class=&quot;audio-widget-container&quot;&gt;
&lt;/section&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定