简体   繁体   中英

angular audio play on ngoninit not working

html code:

<audio id="notification" src="../assets/audio/notification.mp3" autoplay="true"
muted="muted"></audio>

ts code:

 let audioPlayer = <HTMLAudioElement> document.getElementById('notification');
      audioPlayer.muted = false;
      audioPlayer.play();  

I am getting error play() failed because the user didn't interact with the document first.

You should not query audioPlayer on init. The audio needs a bit of time to be ready.

You should trigger muted and play when Html Audio can play. ( https://www.w3schools.com/tags/av_event_canplay.asp )

or Using ViewChild decorator. ( https://angular.io/api/core/ViewChild )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM