最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - The screen shutdown error when streaming videos on the film website uses ReactJs - Stack Overflow

programmeradmin6浏览0评论

I am developing a movie website, using HLS.JS to stream videos with M3U8 link. When using the phone device, the screen will be turned off according to the configuration of the phone. For example, in the phone settings to 30s off the screen if not used, when watching the movie also turn off the screen after 30 seconds.

I am using the following code:

let wakeLock = null;
let hiddenVideo = null;

export const requestWakeLock = async () => {
    try {
        if ("wakeLock" in navigator) {
            // Wake Lock API hỗ trợ (Chrome, Edge, Android)
            wakeLock = await navigator.wakeLock.request("screen");
            wakeLock.addEventListener("release", () => { });
        } else if (/iPhone|iPad|iPod|Macintosh/.test(navigator.userAgent)) {
            // Nếu là Safari hoặc iOS, dùng cách chạy video ẩn
            hiddenVideo = document.createElement("video");
            hiddenVideo.src = "data:video/mp4;base64,AAAA"; // Video trống
            hiddenVideo.loop = true;
            hiddenVideo.play().catch((err) => console.warn("Không thể phát video:", err));
        } else {
            console.warn("Wake Lock API không được hỗ trợ.");
        }
    } catch (err) {
        console.error(`Wake Lock error: ${err.message}`);
    }
};

export const releaseWakeLock = async () => {
    if (wakeLock) {
        await wakeLock.release();
        wakeLock = null;
    }
    if (hiddenVideo) {
        hiddenVideo.pause();
        hiddenVideo.remove();
        hiddenVideo = null;
    }
};

But it seems it is ineffective, because sometimes it works, sometimes it doesn't work. I find the majority does not work on iOS. Hope everyone help. My website is Thank you so much!!!

发布评论

评论列表(0)

  1. 暂无评论