42

In an iOS app you can set application.idleTimerDisabled = YES to prevent the phone from auto locking.

I need to do this in mobile safari for a game like Doodle Jump where the user may not touch the screen for an extended period of time. Is there any documented method or hack to do this?

(Update) They seem to be doing it somehow in this site http://www.uncoveryourworld.com. Visit from your iphone and when you get to the buildings/street scene with music playing in the background just leave your phone alone. It never goes to sleep.

(Update 2) I've spent some time taking a closer look at how they might be keeping the phone from going to sleep. I've done a barebones test and it seems that the way they are looping the audio in the street scene is what keeps it from going to sleep. If you'd like to test this just put a simple audio player that loops on your page and click play:

<audio src="loop.mp3" onended="this.play();" controls="controls" autobuffer></audio>

Everywhere I searched it is being said that this isn't possible, so it is nice to see there is at least some way to do it even if a bit of a hack. Otherwise a browser based game with doodle-jump style play would not be possible. So you could have a loop in your game/app if appropriate or just play a silent loop.

forrex
  • 525
  • 1
  • 4
  • 10
  • Possible duplicate of http://stackoverflow.com/questions/7477424/html5-app-screen-sleep-timeout-on-ipad – Emil Mar 14 '12 at 20:41
  • I didn't know about application.idleTimerDisabled = YES for iOS programming (or it didn't exist in iOS3 when I did this) and so I used to play a silent audio tone every minute or so. A delay between plays should increase battery life rather over your constant loop. (But you'd need to test that it still keeps it awake) – Andiih Dec 13 '12 at 10:30
  • 1
    I tired adding the audio loop, but the phone still goes to sleep. – Tom Kincaid Feb 05 '13 at 18:57
  • Any updates on this? I want my web app to stay "awake" without it always auto-locking. – Jared Jun 09 '13 at 07:35
  • None of the suggestions on this page so far work for me, except actually disabling lock in general settings on the iOS device. Tested on iOS 7.1.2. If any of these worked, they worked due to a bug in iOS which must have been fixed. This includes the site from the question. – Domchi Oct 24 '14 at 16:45
  • 2
    This seems to work well on both iOS and Android: https://github.com/richtr/NoSleep.js – George Strakhov Nov 07 '15 at 10:05

8 Answers8

13

NoSleep.js seems to work in iOS 11 and it reportedly works on Android as well.


Old answer

This is a simple HTML-only method to do that: looping inline autoplaying videos (it might also work in Android Chrome 53+)

<video playsinline muted autoplay loop src="https://rawgit.com/bower-media-samples/big-buck-bunny-480p-30s/master/video.mp4" height=60></video>

See the same demo on CodePen (includes a stopwatch)

Notes

  • Avoid loading a big video just for this. Perhaps make a short, tiny, black-only video or use
  • To make it fully work, the videos needs to be always in the viewport or you need to start its playback via JS: video.play()
fregante
  • 29,050
  • 14
  • 119
  • 159
8

Edit: This work around no longer works. It is not currently possible to prevent the phone from sleeping in safari.

Yes, you can prevent the phone to sleep using an audio loop. The trick won't start automatically, you will have to play it when the visitor touches the screen.

<audio loop src="http://www.sousound.com/music/healing/healing_01.mp3"></audio>

Test page: tap play and the display will stay on but it will dim on some devices, like an iPhone with iOS 7.

Note: be careful using this trick because it will stop any music that the visitors might be using—and it will annoy them.

fregante
  • 29,050
  • 14
  • 119
  • 159
TargunTech
  • 1,132
  • 11
  • 19
  • Is this entire thing is needed to keep the screen from turning off? Which part is the crucial part? – eykanal May 06 '14 at 01:18
  • @eykanal I cleaned it up. An ` – fregante Jun 17 '14 at 02:13
  • 1
    Shouldn't a looped VIDEO do the trick? Android definitely does not go to sleep when watching a video in Chrome (e.g. on YouTube, meaning the website). Does iOS?? – matteo Feb 28 '16 at 20:17
3

[edit] random bug behavior, sometimes lockscreen media controls showing, sometimes not

Years later, updated my code

Easy steps :

  • unlock audio context
  • create silent sound
  • loop it and play forever
  • keep tab active

Working on Safari iOs 15.3.1, tab & browser in background, screen off

// unlock audio context
let ctx = null;

// create silent sound
let bufferSize = 2 * ctx.sampleRate, 
    emptyBuffer = ctx.createBuffer(1, bufferSize, ctx.sampleRate), 
    output = emptyBuffer.getChannelData(0);

// fill buffer
for(let i = 0; i < bufferSize; i++) 
    output[i] = 0;

// create source node
let source = ctx.createBufferSource();
source.buffer = emptyBuffer;
source.loop = true;

// create destination node
let node = ctx.createMediaStreamDestination();
source.connect(node);

// dummy audio element
let audio = document.createElement("audio");
audio.style.display = "none";
document.body.appendChild(audio);

// set source and play
audio.srcObject = node.stream;
audio.play();

// background exec enabled
nicopowa
  • 459
  • 3
  • 11
2

No, you can't do this, unfortunately. The only way to achieve this is by making a UIWebView-application and setting the variable you provided there.
https://stackoverflow.com/a/7477438/267892

Community
  • 1
  • 1
Emil
  • 7,220
  • 17
  • 76
  • 135
0

Even if this approach might not be suitable in every case, you can prevent your phone from locking by reloading the page using Javascript.

// This will trigger a reload after 30 seconds
setTimeout(function(){
    self.location = self.location
}, 30000);

Please note that I tested this with iOS7 beta 3

Besi
  • 22,579
  • 24
  • 131
  • 223
  • 1
    It does in Safari iOS 7.1.2 **and** iOS 8. Demo: http://jsbin.com/nekizeloca/1 It doesn't work in `WebView`s like Chrome and Facebook, though. – fregante Oct 24 '14 at 18:34
  • I'm testing in Safari iOS 7.1.2, on iPhone 4s and iPad and I can't get it to work with this demo as well... – Domchi Oct 26 '14 at 23:52
  • What about using iframe and setting the source of iframe over and over? Kinda faking reload without to really reload? – Oleg Isonen May 30 '16 at 20:11
0

You can stop sleeping and screen dimming in iOS Safari by faking a refresh every 20–30 seconds

var stayAwake = setInterval(function () {
    location.href = location.href; //try refreshing
    window.setTimeout(window.stop, 0); //stop it soon after
}, 30000);

Please use this code responsibly, don't use it "just because". If it's only needed for a bit, disable it.

clearInterval(stayAwake); //allow device sleep again when not needed

Tested in Safari iOS 7, 7.1.2, and 8.1, but it may not work in UIWebView browsers like Chrome for iOS or the Facebook app.

Demo: http://jsbin.com/kozuzuwaya/1

fregante
  • 29,050
  • 14
  • 119
  • 159
  • 1
    It works fine on iOS 7.1.2 and iOS 8. Try the [demo](http://jsbin.com/kozuzuwaya/1). Both of my devices are awake after 4 minutes even though the auto-lock timer is at 1 minute. – fregante Oct 24 '14 at 18:33
  • demo doesn't work for me on both iPad and iPhone, iOS 7.1.2. Don't know why, auto-lock is respected. – Domchi Oct 26 '14 at 23:47
  • 1
    There seems to be few issues with this approach. Firstly, when the current URL has hash in it, this will not work. Probably because updating the location that has hash string does not trigger page load. This can be worked around by using a location without hash string (just "/" for example). This works on iOS 8.3, but brings us to the second issue, on Android it obviously doesn't work and seems that when the device goes to sleep, it executes the location change without it being stopped. – Cvuorinen May 19 '15 at 20:16
  • Related to comment above, so what I ended up using is a version of this solution with `location.href = "/";` and then only execute this code on iOS (check OS from user-agent) and this seems to work as far as I have tested it. – Cvuorinen May 19 '15 at 20:23
  • Also, it does not work if you add the page to home screen (probably for the same reason it doesn't work in other browsers), it only works in Safari. – Cvuorinen May 19 '15 at 20:38
  • I just tried this on iOS 10, as of late 2016. It doesn't appear to work for my iPhone device. – Judah Gabriel Himango Dec 13 '16 at 18:17
  • Try my new answer for iOS 10, I just tried that and it works on iOS 10.1 – fregante Dec 14 '16 at 04:25
0

bfred.it's answer works if you replace the audio-tag with a <video src="..." ... playsinline> tag - but only if the page is open in iOS10+ Safari AND the user has started the video. You can hide the video with CSS.

Also, I suspect that this feature will also be removed at some point.

Community
  • 1
  • 1
Apeli
  • 509
  • 7
  • 17
0

This is based on nicopowa's answer, which saves a PWA from being suspended by iOS. (Playing an infinite loop of nothing keeps the app running - even with the screen turned off.)

In order to also make sure that it's triggered by user interaction,
the only thing to change is instead of

let ctx = null

put

let ctx = new AudioContext()
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • You know about the commenting privilege which you do not have, so well that you can even put it into words. You are aware of the rule https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead . In that situation please do not decide to misuse a different mechanism (an answer) for something it is not meant for and which you are not allowed yet to do. – Yunnosch Oct 03 '22 at 15:09
  • I have however done an edit, which I think turns you post into an answer. Please review to find what I might have logically broken. Maybe you can also [edit] to further improve according to [answer]. – Yunnosch Oct 03 '22 at 15:13