javascript - Stop dynamically created audios -


i'm playing audio service in angular can't manage make stop. here's play() method:

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

then, in component created mute() method i'd stop audios playing:

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

however, doesn't display sounds in console.log and, therefore, none of them being paused.

document.getelementsbytagname('audio') not find audio-tags because have not been added document.

you can add audio-elements document line of code @ end of play()-method: document.getelementsbytagname("body")[0].appendchild(audio);

alternatively can keep audio object in array , make array available component instead of using document.getelementsbytagname('audio'). approach considered preferable on direct dom manipulation in angular applications.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -