Skip to content

Instantly share code, notes, and snippets.

@jonathanlurie
Created September 17, 2017 20:31
Show Gist options
  • Save jonathanlurie/6414ffcd93e9c7dc5998b0c6416173c8 to your computer and use it in GitHub Desktop.
Save jonathanlurie/6414ffcd93e9c7dc5998b0c6416173c8 to your computer and use it in GitHub Desktop.
play audio in JS
<html>
<head>
</head>
<body>
<script>
// index of the sound currently being played (-1 if none)
var playingIndex = -1;
// duration in second of the fading
var fadeDuration = 2;
// duration in ms of the refreshing of the fading
var intervalRefreshMs = 100;
var soundPaths = [
'home-at-last.mp3',
'kendrick.mp3'
];
var soundList = new Array( soundPaths.length );
for(var i=0; i<soundList.length; i++){
var aSound = new Audio(soundPaths[i]);
aSound.loop = true;
// works
aSound.onprogress = function(p){
console.log(p);
}
soundList[i] = aSound;
}
soundList[0].oncanplay = function( info ){
console.log(">>>>>>>>>> CAN PLAY ");
console.log( info );
playFadeIn(0);
}
/*
var current = new Audio('home-at-last.mp3');
current.play();
console.log( current );
var fadeAudio = setInterval(function () {
// Only fade if past the fade out point or not at zero already
if ((sound.currentTime >= fadePoint) && (sound.volume != 0.0)) {
sound.volume -= 0.1;
}
// When volume at zero stop all the intervalling
if (sound.volume === 0.0) {
clearInterval(fadeAudio);
}
}, 200);
function play( index ){
}
*/
function playFadeIn( index ){
if( index < 0 || index >= soundList.length ){
console.warn("The index of the sound to play is out of bound.");
return;
}
var sound = soundList[index];
// abort is the very same song is already playing
if( index === playingIndex && !sound.paused ){
console.log("Sound already playing.");
return;
}
sound.volume = 0.0;
sound.play();
playingIndex = index;
var fadeInInterval = setInterval(function () {
var newVolume = sound.volume + intervalRefreshMs / (1000 * fadeDuration);
if( newVolume >= 1 ){
sound.volume = 1.0;
clearInterval(fadeInInterval);
}else{
sound.volume = newVolume;
}
}, intervalRefreshMs);
}
function pauseFadeOut( forceStop = false ){
if( playingIndex === -1 )
return;
var sound = soundList[playingIndex];
var fadeOutInterval = setInterval(function () {
var newVolume = sound.volume - intervalRefreshMs / (1000 * fadeDuration);
if( newVolume <= 0 ){
sound.volume = 0;
if( forceStop )
sound.stop();
else
sound.pause()
clearInterval(fadeOutInterval);
}else{
sound.volume = newVolume;
}
}, intervalRefreshMs);
}
function goToNextSound( nextSoundIndex = -1 ){
if( playingIndex == nextSoundIndex){
console.log("same sound playing");
return;
}
// fadeout the current one
if( playingIndex !== -1 ){
pauseFadeOut();
}
if( nextSoundIndex == -1 ){
//defining the new playing index
nextSoundIndex = playingIndex == (soundList.length - 1) ? 0 : playingIndex + 1;
console.log( "auto next ");
}
playFadeIn( nextSoundIndex );
}
function pause(){
pauseFadeOut()
}
function resume(){
playFadeIn( playingIndex );
}
//playFadeIn(0);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment