Skip to content

Instantly share code, notes, and snippets.

@nbeloglazov
Last active December 11, 2016 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbeloglazov/828305a48e9ada939abf25ff30ce1cca to your computer and use it in GitHub Desktop.
Save nbeloglazov/828305a48e9ada939abf25ff30ce1cca to your computer and use it in GitHub Desktop.
Script for Rachel's English site to play all audio recordings in soundboards session. Each recording will be played 3 times with pauses for you to repeat. How to use: 1. go to page with audio recordings 2. in url type "javascript:" and paste this script after colon 3. hit enter, it should start playing recordings one by one
function playAndWait(el) {
return new Promise((resolve) => {
el.play().then(() => {
setTimeout(resolve, (el.duration * 2.5) * 1000);
});
});
}
function swapEveryPair(els) {
const elements = Array.from(els);
for (let i = 0; i < elements.length; i += 2) {
const tmp = elements[i];
elements[i] = elements[i + 1];
elements[i + 1] = tmp;
}
return elements;
}
function playNTimes(audioTag, n) {
let res = Promise.resolve();
for (let i = 0; i < n; i++) {
res = res.then(() => playAndWait(audioTag));
}
return res;
}
function playAll(audioTags, timesToPlay) {
return audioTags.reduce((promise, tag) => {
return promise.then(() => playNTimes(tag, timesToPlay));
}, Promise.resolve());
}
function getAllAudio() {
return Array.from(document.querySelectorAll('audio'));
}
function playAllAudio(timesToPlay) {
return playAll(swapEveryPair(getAllAudio()), timesToPlay);
}
playAllAudio(3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment