Variable speed audio playback using HTML5 range slider

Variable speed audio playback using HTML5 range slider

by Nicholas Bovee -
Number of replies: 2

Below is a bit of script that allows the user to finely control audio playback speed using the HTML5 range slider. (I don't know if this is old news for people, but I just became aware of the HTML5 range slider yesterday and think it's pretty useful.)

I first came across this post that showed how to set up a 3-button audio speed changer: https://moodle.org/mod/forum/discuss.php?d=261710&parent=1134187

Then I read about the HTML5 range slider here: http://thenewcode.com/757/Playing-With-The-HTML5-range-Slider-Input

Putting 2 and 2 together, I did this (ignore all the inline styling if you want):


<p><audio id="audio1" controls="true" style="width: 100%;">

<source src="http://youraudiofile.mp3">

Your browser does not support HTML5.</audio></p>

<div style="white-space: nowrap; max-width: 3000px; margin-top: -15px;">

<label for="slider" style="font-size: 0.9em;">Speed</label>

<input type="range" min="0.5" max="1.5" step=".01" value="1" id="slider" oninput="outputUpdate(value)" onchange="outputSpeed(value)" style="width: 75%;">

<output for="slider" id="playbackrate" style="font-size: 0.9em;">1.0</output>x</div>

<script>

function outputUpdate(speedDisplay) {

document.querySelector('#playbackrate').value = speedDisplay;

}

myAudio = document.getElementById("audio1")

function outputSpeed() { 

        myAudio.playbackRate = document.querySelector('#playbackrate').value;

}

</script>


Copy the code above into a Moodle page, replace the audio link (or insert one), and you're good to go. You can change the values after "input" to whatever you'd like. e.g. Change max="1.5" to max="2" if you want the maximum playback rate to be 2x the original speed.

This might be a good candidate for the Generico filter to make repeated use easier.

Copy and paste the CSS from the following site to make the slider look nice: http://brennaobrien.com/blog/2014/05/style-input-type-range-in-every-browser.html

The result looks like this in Chrome (Moodle 3.2):


No idea about cross browser compatibility, but it seems to work fine in Chrome and iOS.

Please let me know if there's a better or more elegant way to go about it as I'm certainly no expert!

Average of ratings: Useful (1)
In reply to Nicholas Bovee

Re: Variable speed audio playback using HTML5 range slider

by Justin Hunt -
Picture of Particularly helpful Moodlers Picture of Plugin developers

Hi Nic

Top stuff. This is indeed a good candidate for a Generico template or VideoEasy template.

When I get a chance I will make one up.

J

In reply to Justin Hunt

Re: Variable speed audio playback using HTML5 range slider

by Nicholas Bovee -
Hi Justin! I installed your Generico this morning and fiddled with it just very briefly, but I haven't gotten it to work right yet. It would be great if you could make it! smile It looks very useful.