Prevent Android chrome from going idle / auto-locking / sleeping phone?
We strongly don't encourage developers to do this at all. However it is possible. You can simply have a video playing on the page and the device won't go to sleep. This means you could have single frame video set to auto-loop and play (requires a user interaction)
Richard Tibbett has created NoSleep.js to simplify the process for developers.
JavaScript in Chrome on Android (7.0) indeed shuts down after 5 min in sleep mode. Aaargh!
To prevent that, we need e.g. an audio object:
<audio id="dummyAudio">
<source src="silent.ogg" type="audio/ogg">
<source src="silent.mp3" type="audio/mpeg">
</audio>
and play it at regular intervals:
function playDummyAudio() { dummyAudio.play(); }
$(function() {
var dummyAudio = document.querySelector('#dummyAudio');
window.setInterval(playDummyAudio, 60 * 1000);
}
Note that the Audio object has to be "unlocked" in a user gesture callback. This can be accomplished e.g. by having a grey CSS overlay with a big fat dummy "Start" button, whose onClick()
callback only hides the overlay and calls dummyAudio.load()
.