Managing display and power sleep mode

In some scenarios, we may need to control the machine or block the machine from entering into sleep mode. For example, if you are developing a media player application, the system should not go to sleep mode when playing a video, or your app should never turn off the power when playing the audio. Electron's powerSaveBlocker module gives you the access to the power and display mode. You can use this module to prevent the system from entering the low power mode. As we discussed, a classic example of this scenario is a video player. When the user clicks on the play button, you can ask this module to prevent low power mode, and you can stop monitoring the power state change when the user stops the playback. Here is an example for the powerSaveBlocker module:

<html>
<head>
</head>
<body>
<script language="javascript">
const { powerSaveBlocker } = require('electron').remote;
document.addEventListener("DOMContentLoaded", init, false);

function init() {
var video = document.getElementById("video");
var _ref = null;
video.addEventListener("play", () => {
_ref = powerSaveBlocker.start('prevent-display-sleep');
});

video.addEventListener("stop", () => { powerSaveBlocker.stop(_ref);
});
}

</script>
<video id='video' controls preload='none'
poster="http://media.w3.org/2010/05/sintel/poster.png">
<source id='mp4'
src="http://media.w3.org/2010/05/sintel/trailer.mp4"
type='video/mp4'>
<p>Your user agent does not support the HTML5 Video element. </p>
</video>
</body>
</html>

The start method accepts type argument, which can accept two values:

  • prevent-app-suspension
  • prevent-display-sleep

prevent-app-suspension prevents the application from being suspended. This will keep the system active, but allow the screen to be turned off. This can be used when you download a file or playing an audio. To keep display active, use prevent-display-sleep value. This will return an integer value representing the blocker id. This blocker id should be passed to the stop method to stop powerSaveBlocker from watching the power state change.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.194