Skip to content

Instantly share code, notes, and snippets.

@jtangelder
Last active June 5, 2017 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtangelder/14654b34a34e6dfa4c5f32515c0308ce to your computer and use it in GitHub Desktop.
Save jtangelder/14654b34a34e6dfa4c5f32515c0308ce to your computer and use it in GitHub Desktop.
Battery level volume control
<!DOCTYPE html>
<html>
<body>
<h1>Battery level volume control</h1>
<div id="player"></div>
<script src="https://www.youtube.com/iframe_api"></script>
<script>
let player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: 390,
width: 640,
videoId: 'dQw4w9WgXcQ',
events: {onReady: onPlayerReady}
});
}
function unsupported() {
alert('Sorry, could not get the battery level!');
}
function onPlayerReady(event) {
if (!navigator.getBattery) {
unsupported();
return;
}
navigator.getBattery().then(battery => {
const setVolume = () => {
player.setVolume(battery.level * 100);
};
battery.onlevelchange = setVolume;
setVolume();
player.playVideo();
}, unsupported);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment