Last active
June 5, 2017 11:48
-
-
Save jtangelder/14654b34a34e6dfa4c5f32515c0308ce to your computer and use it in GitHub Desktop.
Battery level volume control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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