Commit 794796dd authored by Skye Yu's avatar Skye Yu

use wake lock and setInterval

parent 8a9552fa
...@@ -705,6 +705,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress) ...@@ -705,6 +705,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
for (let i = 0; i < filename.length; i++) fname.push(filename.charCodeAt(i)); for (let i = 0; i < filename.length; i++) fname.push(filename.charCodeAt(i));
let wakeLock = null; let wakeLock = null;
let timer = null;
async function visibilitychange() { async function visibilitychange() {
let audio = document.getElementById('test_audio'); let audio = document.getElementById('test_audio');
if (!audio) { if (!audio) {
...@@ -720,18 +721,23 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress) ...@@ -720,18 +721,23 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
if (document.visibilityState === 'hidden') { if (document.visibilityState === 'hidden') {
console.log('Page is hidden'); console.log('Page is hidden');
audio.play(); audio.play();
if ('wakeLock' in navigator) { timer = setInterval(() => {
try { console.log("Keeping the page alive...");
wakeLock = await navigator.wakeLock.request('screen'); }, 1000)
console.log('Wake lock activated');
} catch (err) {
console.error('Failed to acquire wake lock:', err);
}
}
} else { } else {
audio.pause(); audio.pause();
console.log('Page is visible'); console.log('Page is visible');
wakeLock && wakeLock.release(); wakeLock && wakeLock.release();
timer && clearInterval(timer)
}
}
if ('wakeLock' in navigator) {
try {
wakeLock = await navigator.wakeLock.request('screen');
console.log('Wake lock activated');
} catch (err) {
console.error('Failed to acquire wake lock:', err);
} }
} }
...@@ -739,6 +745,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress) ...@@ -739,6 +745,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
const audio = document.getElementById('test_audio'); const audio = document.getElementById('test_audio');
audio && audio.pause() && audio.remove(); audio && audio.pause() && audio.remove();
wakeLock && wakeLock.release(); wakeLock && wakeLock.release();
timer && clearInterval(timer)
} }
visibilitychange(); visibilitychange();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment