Don't you think this might be a flawlles solution to Check if Audio() is playing?

人盡茶涼 提交于 2020-05-17 07:28:13

问题


Do you think this might be a flawlles solution to Check if Audio() is playing? Like when handling a stratup theme ...

Place this above in the head

songStarted = false;
song = new Audio();
song.src = 'audio/startuptheme.mp3';    
song.addEventListener('playing', (event) => {songStarted = true;}); 
song.play(); 

And use the following script anywhere (where first user interaction should happen), as many times as you need

if (!songStarted){   
      if (typeof song == 'undefined'){
          song = new Audio();
          song.src = 'audio/startuptheme.mp3';  
      }
      song.addEventListener('playing', (event) => {songStarted = true;}); 
      song.play(); 
} 

I dont think any one can come with a better solution, unless it's an equally flawless solution without the use of globals vars.

来源:https://stackoverflow.com/questions/61407319/dont-you-think-this-might-be-a-flawlles-solution-to-check-if-audio-is-playing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!