Stop dynamically created audios

Viewed 169

I'm playing an audio from a service in Angular but I can't manage to make it stop. Here's my play() method:

play(item: string): void {
  const audio = new Audio();
  audio.src = item;
  audio.load();
  audio.play();
}

Then, in my component I created a mute() method where I'd like to stop all audios currently playing:

mute(): void {
  const sounds = document.getElementsByTagName('audio');
  console.log(sounds);
  for (let i = 0; i < sounds.length; i++) {
    sounds[i].pause();
  }
}

However, it doesn't display any sounds in my console.log and, therefore, none of them are being paused.

2 Answers
Related