Skip to content

Instantly share code, notes, and snippets.

@jtangelder jtangelder/index.html
Last active Jun 5, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.