Stefano

Howler.js lock screen...

Recommended Posts

Hi, I am new in this forum and I hope to find an answer for my problem.

I have developed a game that will runs in a webview under Android, but when the music is playing and we press 'lock button' phone, the screen is black but the music runs again.

I have added all check to know if the HTML lost focus, but nothing works... Do you have some idea?

 

Thanks!!! 😅

 

This is the coded added for check when the HTML lost focus (works fine in browser but under webview no :( )

<script type="text/javascript">

 
// Howler.mobileAutoEnable = true;
 
controls.mute = function () {
 
if (typeof Howler !== 'undefined' && typeof Howler.mute === 'function') {
    Howler.mute(true);
    return 'controls: done';
}
 
    return 'controls: failed';
};
 
controls.unmute = function () {
 
    if (typeof Howler !== 'undefined' && typeof Howler.mute === 'function') {
        Howler.mute(false);
       return 'controls: done';
    }
    return 'controls: failed';
};
 
//----------------------------------------------------------------------------------------------------
// Set the name of the hidden property and the change event for visibility
var hidden, visibilityChange;
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
    hidden = "hidden";
    visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
    hidden = "msHidden";
    visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {. 
    hidden = "webkitHidden";
    visibilityChange = "webkitvisibilitychange";
}
 
function handleVisibilityChange() {
 
    if (document[hidden]) {
       controls.mute();
    } else {
       controls.unmute();
    }
}
 
// Warn if the browser doesn't support addEventListener or the Page Visibility API
if (typeof document.addEventListener === "undefined" || hidden === undefined) {
    console.log("VisibilityChange requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
} else {
// Handle page visibility change
    document.addEventListener(visibilityChange, handleVisibilityChange, false);
}
//----------------------------------------------------------------------------------------------------
 
//----------------------------------------------------------------------------------------------------
window.onfocus = function () {
 
if(typeof controls !== 'undefined' && controls != null)
    if(controls.unmute != null)
        controls.unmute();
}
 
window.onblur = function () {
 
   if(typeof controls !== 'undefined' && controls != null)
       if(controls.unmute != null
        controls.mute();
}
//----------------------------------------------------------------------------------------------------
 
//----------------------------------------------------------------------------------------------------
// Handle page visibility change events
function handleVisibilityChange() {
 
    if (document.visibilityState == "hidden") {
        controls.mute();
    } else {
        controls.unmute();
    }
}
 
document.addEventListener('visibilitychange', handleVisibilityChange, false);
//----------------------------------------------------------------------------------------------------
 
</script>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.