Play html5 sound when phone screen is turned off

I am creating a mobile optimized website and would like to play sound after the counter is finished. Usually after 2 or more minutes.

The problem is that after the phone screen is off, the phone turns off my js and prevents the sound from playing.

Potential solution:

I read this. In solution No. 3, u can sequentially loop 2 tracks, and the mobile phone will play both, even when the screen is off. So, the trick I found is to use this loop, but it has the first sound: 1. Be silent. Yes, a complete audible alarm is 5 minutes 2. ask him to loop as many times as needed to complete the second song.

So, for example, here sound without sound will be played twice before another sound starts playing. The trick is to control the variable max to achieve the desired result.

 var count = 0; var max = 2; document.getElementById('silence_audio').addEventListener('ended', function () { this.currentTime = 0; this.pause(); if (count < max) { document.getElementById('silence_audio').play(); count++; } else { document.getElementById('audio_4').play(); } }, false); document.getElementById('audio_4').addEventListener('ended', function () { this.currentTime = 0; this.pause(); document.getElementById('silence_audio').play(); }, false); 

But my question is, are there any other better solutions?

Thanks!

+6
source share

Source: https://habr.com/ru/post/947720/


All Articles