A Pen by Christian Heilmann on CodePen.
Created
September 25, 2022 08:44
-
-
Save dr-kd/05025e9affc6b0b3df7161e40ebb359f to your computer and use it in GitHub Desktop.
Audio with speed 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
<div class="speedcontrolcontainer"> | |
<audio src="https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_2MG.mp3" controls></audio> | |
<div> | |
<label for="pbrate">Speed:</label> | |
<input type="range" id="pbrate" min=.5 max=3 value=1 step=.1> | |
<span></span> | |
</div> | |
</div> |
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
const audio = document.querySelector('.speedcontrolcontainer audio'); | |
const playbackrate = document.querySelector('.speedcontrolcontainer input'); | |
const display = document.querySelector('.speedcontrolcontainer span'); | |
const displayvalue = val => { | |
return parseInt(val * 100, 10) + '%' | |
} | |
if (window.localStorage.pbspeed) { | |
audio.playbackRate = window.localStorage.pbspeed; | |
playbackrate.value = window.localStorage.pbspeed; | |
} | |
display.innerText = displayvalue(audio.playbackRate); | |
playbackrate.addEventListener('change', e => { | |
audio.playbackRate = playbackrate.value; | |
display.innerText = displayvalue(playbackrate.value); | |
window.localStorage.pbspeed = playbackrate.value; | |
}); |
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
.speedcontrolcontainer { | |
max-width: 30em; | |
display: block; | |
border: 1px solid #000; | |
padding: 10px; | |
font-family: Sans-serif; | |
} | |
.speedcontrolcontainer audio { | |
width: 100%; | |
display: block; | |
} | |
.speedcontrolcontainer div { | |
display: flex; | |
padding: .5em 0; | |
gap: 5px; | |
} | |
.speedcontrolcontainer label { | |
flex: 1 | |
} | |
.speedcontrolcontainer input[type="range"] { | |
flex: 5 | |
} | |
.speedcontrolcontainer span { | |
flex: 1; | |
text-align: center; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment