Last active
November 1, 2018 03:48
-
-
Save spekkionu/4663f3e9deea9c34172a20dee462ee41 to your computer and use it in GitHub Desktop.
Audio player component
This file contains hidden or 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
/** | |
* Copyright 2018 Jonathan Bernardi | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
class AudioPlayer extends HTMLElement { | |
constructor() { | |
super(); | |
this.playing = false; | |
this.loaded = false; | |
this.time = '00:00'; | |
this.duration = '00:00'; | |
this.progress = 0; | |
this.shadow = null; | |
this.src = this.getAttribute("src"); | |
this.volume = this.hasAttribute('volume') ? this.getAttribute("volume") : 100; | |
this.title = this.getAttribute("title") || ''; | |
this.loop = this.hasAttribute("loop"); | |
this.autoplay = this.hasAttribute("autoplay"); | |
this.player = null; | |
} | |
connectedCallback() { | |
this.shadow = this.attachShadow({mode: 'open'}); | |
this.render(); | |
} | |
render() { | |
let style = document.createElement('style'); | |
style.setAttribute('shim-shadowdom', true); | |
style.type = 'text/css'; | |
if (style.styleSheet) { | |
style.styleSheet.cssText = this.style(); | |
} else { | |
style.appendChild(document.createTextNode(this.style())); | |
} | |
let container = document.createElement("div"); | |
container.setAttribute('class', 'player'); | |
let play = document.createElement('button'); | |
play.setAttribute('class', 'player-play'); | |
play.setAttribute('type', 'button'); | |
play.setAttribute('role', 'button'); | |
play.innerHTML = '<svg class="icon" aria-hidden="true" data-icon="play" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="currentColor" d="M8 5v14l11-7z"/><path d="M0 0h24v24H0z" fill="none"/></svg>'; | |
play.addEventListener('click', e => this.play()); | |
container.appendChild(play); | |
let pause = document.createElement('button'); | |
pause.setAttribute('class', 'player-pause'); | |
pause.setAttribute('type', 'button'); | |
pause.setAttribute('role', 'button'); | |
pause.setAttribute('style', 'display:none;'); | |
pause.innerHTML = '<svg class="icon" aria-hidden="true" data-icon="pause" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0V0z"/><path fill="currentColor" d="M6 19h4V5H6v14zm8-14v14h4V5h-4z"/></svg>'; | |
pause.addEventListener('click', e => this.pause()); | |
container.appendChild(pause); | |
let seek = document.createElement('div'); | |
seek.setAttribute('class', 'player-seeker'); | |
seek.addEventListener('click', e => this.seek(e)); | |
let title = document.createElement('div'); | |
title.setAttribute('class', 'player-title'); | |
title.innerText = this.title; | |
seek.appendChild(title); | |
let time = document.createElement('div'); | |
time.setAttribute('class', 'player-time'); | |
seek.appendChild(time); | |
let elapsed = document.createElement('span'); | |
elapsed.setAttribute('class', 'player-elapsed'); | |
elapsed.innerText = this.time; | |
time.appendChild(elapsed); | |
let separator = document.createElement('span'); | |
separator.innerText = '/'; | |
time.appendChild(separator); | |
let duration = document.createElement('span'); | |
duration.setAttribute('class', 'player-duration'); | |
duration.innerText = this.duration; | |
time.appendChild(duration); | |
let location = document.createElement('div'); | |
location.setAttribute('class', 'player-location'); | |
seek.appendChild(location); | |
container.appendChild(seek); | |
this.player = document.createElement('audio'); | |
this.player.setAttribute('src', this.src); | |
this.player.setAttribute('preload', 'metadata'); | |
this.player.setAttribute('style', 'display:none;'); | |
// this.player.setAttribute('controls', null); | |
if (this.loop) { | |
this.player.setAttribute('loop', null); | |
} | |
if (this.autoplay) { | |
this.player.setAttribute('autoplay', null); | |
} | |
this.shadow.appendChild(style); | |
this.shadow.appendChild(this.player); | |
this.shadow.appendChild(container); | |
this.player.addEventListener('loadedmetadata', e => { | |
this.duration = this.formatDuration(this.player.duration); | |
this.shadow.querySelector('.player-duration').innerText = this.duration; | |
this.loaded = true; | |
}); | |
this.player.addEventListener('play', e => { | |
this.playing = true; | |
this.shadow.querySelector('.player-play').style.display = 'none'; | |
this.shadow.querySelector('.player-pause').style.display = 'block'; | |
}); | |
this.player.addEventListener('pause', e => { | |
this.playing = false; | |
this.shadow.querySelector('.player-pause').style.display = 'none'; | |
this.shadow.querySelector('.player-play').style.display = 'block'; | |
}); | |
this.player.addEventListener('timeupdate', e => { | |
this.time = this.formatDuration(this.player.currentTime); | |
this.shadow.querySelector('.player-elapsed').innerText = this.time; | |
this.duration = this.formatDuration(this.player.duration); | |
this.shadow.querySelector('.player-duration').innerText = this.duration; | |
this.progress = this.player.currentTime / this.player.duration * 100; | |
this.shadow.querySelector('.player-location').style.width = this.progress + '%'; | |
}); | |
this.player.addEventListener('volumechange', e => { | |
this.volume = this.player.volume * 100; | |
}); | |
} | |
play(e) { | |
this.player.play(); | |
} | |
pause(e) { | |
this.player.pause(); | |
} | |
seek(e) { | |
e.stopPropagation(); | |
this.player.currentTime = (e.clientX - e.currentTarget.offsetLeft) / e.currentTarget.offsetWidth * this.player.duration; | |
; | |
} | |
formatDuration(time) { // 4201 | |
let hours = Math.floor(time / 3600); // 1 | |
time = time % 3600; // 601 | |
let minutes = Math.floor(time / 60); // 10 | |
let seconds = Math.floor(time % 60); // 1 | |
if (hours > 0) { | |
return hours + ':' + this.padNumber(minutes) + ':' + this.padNumber(seconds); | |
} | |
return minutes + ':' + this.padNumber(seconds); | |
} | |
padNumber(string, length = 2) { | |
return (new Array(length + 1).join("0") + string).slice(-length); | |
} | |
style() { | |
return ` | |
.player { | |
width: 100%; | |
background-color: rgba(0, 0, 0, .75); | |
height: 50px; | |
padding:0; | |
display: flex; | |
flex-direction: row; | |
justify-content: center; | |
align-items: stretch; | |
color: #fff; | |
} | |
audio { | |
display: none; | |
} | |
.icon { | |
width: 40px; | |
height: 40px; | |
} | |
button { | |
border:none; | |
background-color: #00aef6; | |
outline: none; | |
color: #fff; | |
box-shadow: none; | |
display: flex; | |
justify-content: center; | |
align-items: center; | |
padding: 0 10px; | |
} | |
button:focus, | |
button:active { | |
border:none; | |
outline: none; | |
} | |
.player-seeker { | |
position: relative; | |
flex-grow: 1; | |
flex-shrink: 1; | |
display: flex; | |
justify-content: space-between; | |
align-items: center; | |
padding: 10px; | |
text-shadow: 2px 2px 2px rgba(0, 0, 0, 0.75); | |
} | |
.player-location { | |
position: absolute; | |
left:0; | |
top:0; | |
height: 100%; | |
width:0; | |
background-color: #0082b2; | |
z-index: 1; | |
transition: width .2s ease; | |
} | |
.player-title { | |
z-index: 2; | |
user-select: none; | |
margin-right: auto; | |
} | |
.player-time { | |
z-index: 2; | |
user-select: none; | |
} | |
`; | |
} | |
} | |
// Register the element with the browser. | |
customElements.define('audio-player', AudioPlayer); |
This file contains hidden or 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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta name="description" content="Audio Player"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/2.1.3/webcomponents-loader.js"></script> | |
<script src="./src/audio-player.js" type="module" async></script> | |
<title>Audio Player</title> | |
</head> | |
<body> | |
<div style="width: 800px;"> | |
<audio-player title="Audio File Title" volume="25" src="/path/to/audio/file"></audio-player> | |
</div> | |
</body> | |
</html> |
This file contains hidden or 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
{ | |
"name": "audio-player", | |
"version": "1.0.0", | |
"main": "audio-player.js", | |
"author": "Jonathan Bernardi <[email protected]>", | |
"license": "MIT", | |
"dependencies": { | |
"@webcomponents/webcomponentsjs": "latest" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment