123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <audio src="https://silvercherry.fr/sources/musics/Angevin_B.mp3" preload="none" controls>
  Votre navigateur ne supporte pas la balise audio.
</audio>

<!-- For the playlist, you just need to do like this : -->

<div class="playlist">
  <audio src="https://silvercherry.fr/sources/musics/Celtic_Impulse.mp3" preload="none" controls>
    Votre navigateur ne supporte pas la balise audio.
  </audio>
  <ul>
    <li id="https://silvercherry.fr/sources/musics/Celtic_Impulse.mp3">Celtic Impulse</li>
    <li id="https://silvercherry.fr/sources/musics/Skye_Cuillin.mp3">Skye Cuillin</li>
    <li id="https://silvercherry.fr/sources/musics/Errigal.mp3">Errigal</li>
    <li id="https://silvercherry.fr/sources/musics/Fiddles_McGinty.mp3">Fiddles McGinty</li>
  </ul>
</div>

<p>All musics - Kevin MacLeod (<a href="https://incompetech.com/wordpress/">incompetech.com</a>)<br>
	Licensed under <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons: By Attribution 4.0 License</a></p>
            
          
!
            
              "use strict";

// https://web.silvercherry.fr/player-en.html
// Version 19.27.3 (Year, week, version)

// Génération du player :
var audio = document.getElementsByTagName("audio");
var player = '<div class="player"><div class="play" title="Play"><span></span></div><div class="pause" title="Pause" style="display: none;"><span></span></div><div class="backward" title="Backward"><span></span><span></span></div><div class="forward" title="Forward"><span></span><span></span></div><div class="title"></div><div class="prog"><div class="progBarre"></div></div><div class="timer">00:00</div><div class="volumeF" title="Volume"><div class="volumeBarre"></div></div></div>';
var head = document.getElementsByTagName("head")[0];
var style = document.createElement("style");
style.type = "text/css";
style.innerHTML = ".player .timer{white-space:nowrap}.player,.player *{box-sizing:border-box;margin:0;padding:0;line-height:1}audio{display:none}.player{display:flex;align-items:center;margin:15px;padding:5px;background-color:#FBFBFB;border-style:solid;border-width:2px 0;border-color:#333;border-radius:5px;flex-wrap:wrap;justify-content:space-around}.player .pause span,.player .play span,.playlist .forward span,.playlist .backward span{display:inline-block;height:20px;border-style:solid;width:15px}.player .play span,.playlist .forward span:first-of-type{border-width:10px 0 10px 15px;border-color:transparent transparent transparent #333}.player .pause span{border-width:0 5px;border-color:transparent #333}.player .pause,.player .play,.playlist .forward,.playlist .backward{display:flex;align-items:center;width:15px;height:30px;margin:0 10px;cursor:pointer}.playlist .forward,.playlist .backward{width:20px}.player .play:hover span,.playlist .forward:hover span:first-of-type{border-color:transparent transparent transparent #666}.player .pause:hover span{border-color:transparent #666}.player .prog{overflow:hidden;cursor:pointer;max-width:100%;min-width:100px;height:20px;margin:0 10px;background-color:#CCC;flex-grow:1}.player .prog:hover .progBarre{background-color:#666}.player .progBarre{width:0;background-color:#333;height:20px;overflow:visible;color:#000;text-indent:5px}.player .timer,.playlist{color:#333}.player .volumeF{cursor:pointer;width:75px;height:10px;margin:5px 10px;background-color:#CCC}.player .volumeF:hover .volumeBarre{background-color:#666}.player .volumeBarre{width:100%;background-color:#333;height:10px}.playlist{margin:15px}.playlist .player{border-bottom:0;border-radius:5px 5px 0 0;margin:0}.playlist ul{background-color:#FBFBFB;margin:0;padding:0 5px 5px;list-style-type:none;border-style:solid;border-width:0 0 2px;border-color:#333;border-radius:0 0 5px 5px}.playlist ul li{padding:5px;cursor:pointer}.playlist ul li:nth-of-type(odd){background-color:#CCC}.playlist ul li:nth-of-type(even){background-color:#DDD}.playlist ul li:hover{background-color:#FBFBFB}.playlist .backward span:first-of-type{border:0;border-right:5px solid #333}.playlist .forward span:last-of-type{border:0;border-left:5px solid #333}.playlist .backward span:last-of-type{border-width:10px 15px 10px 0;border-color: transparent #333 transparent transparent}.playlist .backward:hover span:first-of-type,.playlist .forward:hover span:last-of-type{border-color: #666}.playlist .backward:hover span:last-of-type{border-color: transparent #666 transparent transparent}.player .title{margin:5px}.playlist ul li.active{text-decoration:underline;font-style:italic}";
head.appendChild(style);
var play = document.getElementsByClassName("play");
var pause = document.getElementsByClassName("pause");
var backward = document.getElementsByClassName("backward");
var forward = document.getElementsByClassName("forward");

// Mise en place du player 
for (var i = 0; i <= audio.length - 1; i++) {
	audio[i].insertAdjacentHTML("afterend", player);
	// (Et on profite de cette boucle pour check les titles et data-volume)
	if(audio[i].title !== null && audio[i].parentNode.className !== "playlist") {
		audio[i].nextElementSibling.getElementsByClassName("title")[0].innerHTML = audio[i].title;
	} 
	var dataVolume = audio[i].getAttribute("data-volume");
	if (dataVolume 
		&& dataVolume>=0 
		&& dataVolume<=1) {
		audio[i].volume = dataVolume;
		audio[i].nextElementSibling.getElementsByClassName("volumeF")[0].children[0].style.width = (dataVolume*100)+"%";
	}
	// Les boutons forward et backward sont réservés aux playlists, on les supprime des player solo
	if (audio[i].parentNode.className !== "playlist") {
		var deleteForward = audio[i].nextElementSibling.getElementsByClassName("forward")[0];
		audio[i].nextElementSibling.removeChild(deleteForward);
		var deleteBackward = audio[i].nextElementSibling.getElementsByClassName("backward")[0];
		audio[i].nextElementSibling.removeChild(deleteBackward);
	}
	// L'autoplay est un cancer pour les utilisateurs. Si vous souhaitez booster vos écoutes, falsifiez vos chiffres et épargnez vos utilisateurs.
	audio[i].removeAttribute("autoplay");
}

// Fonction play :
for (var i = 0; i <= audio.length - 1; i++) {
	play[i].addEventListener("click", function()
	{
		clearInterval(interval);
		for (var j = 0; j <= audio.length - 1; j++) {
			audio[j].pause();
			audio[j].nextElementSibling.getElementsByClassName("play")[0].style.display = null;
			audio[j].nextElementSibling.getElementsByClassName("pause")[0].style.display = "none";
		}
		this.style.display = "none";
		this.nextElementSibling.style.display = null;
		clearInterval(time);
		if (this.parentNode.parentNode.className === "playlist") {
			Timer(false, true);
		} else {
			Timer();
		}
		this.parentNode.previousElementSibling.play();
	});
}

// Fonction pause :
for (var i = 0; i <= audio.length - 1; i++) {
	pause[i].addEventListener("click", function()
	{
		this.parentNode.previousElementSibling.pause();
		clearInterval(time);
		this.style.display = "none";
		this.previousElementSibling.style.display = null;
	});
}

// Fonction timer :
var time;
var timer = document.getElementsByClassName("timer");

var interval = setInterval(function()
{
	for (var i = 0; i <= audio.length - 1; i++) {
		var duree = audio[i].duration;
		if (audio[i].error && audio[i].error.code === 4) {
			timer[i].innerHTML = "File not found";
		} else if ((!audio[i].hasAttribute("preload") || (audio[i].hasAttribute("preload") && audio[i].getAttribute("preload") !== "none")) && isNaN(duree)) {
			timer[i].innerHTML = "load";
		} else if (audio[i].hasAttribute("preload") && audio[i].getAttribute("preload") === "none") {
			timer[i].innerHTML = "--:--";
		} else {
			var minutes = Math.trunc(duree / 60);
			var seconds = Math.trunc(duree % 60);
			minutes = ("0"+minutes).slice(-2);
			seconds = ("0"+seconds).slice(-2);
			timer[i].innerHTML = minutes+":"+seconds;
		}
	}
}, 1000);
setTimeout(function()
{
	clearInterval(interval);
	for (var i = 0; i <= audio.length - 1; i++) {
		if (timer[i].innerHTML === "load") {
			timer[i].innerHTML = "--:--";
		}
	}
}, 10100);


var progBarre = document.getElementsByClassName("progBarre");
var audiotime = [];
var tempAudiotime = [];

function Timer(stop = false, activatePlaylist = false)
{
	if (stop === true) {
		clearInterval(time);
	} else {
		time = window.setInterval(function() 
		{
			for (var i = 0; i <= audio.length - 1; i++) {
				// Mise en place de l'affichage timer / barre de progression.
				tempAudiotime[i] = audiotime[i];
				audiotime[i] = audio[i].currentTime;
				var minutes = Math.trunc(audiotime[i] / 60);
				var seconds = Math.trunc(audiotime[i] % 60);
				minutes = ("0"+minutes).slice(-2);
				seconds = ("0"+seconds).slice(-2);
				if (tempAudiotime[i] == audiotime[i] && !audio[i].paused) {
					timer[i].innerHTML = "load";
				}	else if (audiotime[i] !== 0) {
					timer[i].innerHTML = minutes+":"+seconds;
				} 
				if (isNaN(audio[i].duration)) {
					timer[i].innerHTML = "--:--";
				}
				if (audio[i].error && audio[i].error.code === 4) {
					reset(audio[i]);
					timer[i].innerHTML = "File not found";
				}
				var widthProg = audio[i].currentTime / audio[i].duration * 100;
				progBarre[i].style.width = widthProg+"%";
				if (audiotime[i] === audio[i].duration && activatePlaylist === false && !audio[i].hasAttribute("loop")) {
					// Stop le fichier audio à la fin des balises qui ne loop pas.
					reset(audio[i]);
					Timer(true);
				} else if (audiotime[i] === audio[i].duration && activatePlaylist === true) {
					// Check l'épisode suivant à lire dans la playlist
					var thisPlaylist = audio[i].parentNode;
					for (var k = 0; k <= playlist.length - 1; k++) {
						if (thisPlaylist === playlist[k]) {
							var nextPlaylistNumber = k;
							playerPlaylist[k].src = next[k];
							for (var j = 0; j <= thisPlaylist.getElementsByTagName("ul")[0].children.length - 1; j++) {
								// Ecriture du titre à venir (premier fichier si fin de playlist)
								var ul = thisPlaylist.getElementsByTagName("ul")[0];
								if (ul.children[j].nextElementSibling !== null && ul.children[j].nextElementSibling.id === next[k]) {
									playerPlaylist[k].nextElementSibling.getElementsByClassName("title")[0].innerHTML = ul.children[j].nextElementSibling.innerHTML;
									if (ul.children[j].nextElementSibling.nextElementSibling) {
										forward[k].id = ul.children[j].nextElementSibling.nextElementSibling.id;
									} else {
										forward[k].id = ul.children[0].id;
									}
									backward[k].id = ul.children[j].id;
									ul.children[j].classList.remove("active");
									ul.children[j].nextElementSibling.classList.add("active");
								} else if (ul.children[j].nextElementSibling === null && ul.children[0].id === next[k]) {
									playerPlaylist[k].nextElementSibling.getElementsByClassName("title")[0].innerHTML = ul.children[0].innerHTML;
									forward[k].id = ul.children[1].id;
									backward[k].id = ul.lastElementChild.id;
									ul.children[j].classList.remove("active");
									ul.children[0].classList.add("active");
								}
							}
						}
					}
					audio[i].play();
					prepareNext(nextPlaylistNumber);
				}
			}
		}, 500);
	}
}

function reset(audioElement) {
	audioElement.currentTime = 0;
	audioElement.pause();
	audioElement.nextElementSibling.getElementsByClassName("progBarre")[0].style.width = "0%";
	audioElement.nextElementSibling.getElementsByClassName("timer")[0].innerHTML = "00:00";
	audioElement.nextElementSibling.getElementsByClassName("play")[0].style.display = null;
	audioElement.nextElementSibling.getElementsByClassName("pause")[0].style.display = "none";
}

// Important : ce code ne doit pas être intégré directement dans Timer, le return est d'une importance capitale car la boucle DOIT s'arrêter si ce n'est pas le dernier élément de la liste. Et un return dans l'interval semble le mettre à mal.
// PS : next() existe nativement. Fonction renommée pour éviter tout problème.
function prepareNext(playlistNumber)
{
	for (var i = 0; i <= playlist.length - 1; i++) {
		var ul = playlist[i].getElementsByTagName("ul")[0];
		for (var j = 0; j <= ul.childElementCount - 1; j++) {
			if (playlistNumber === i 
				&& ul.children[j].id === next[i] 
				&& ul.children[j].nextElementSibling !== null) {
				next[i] = ul.children[j].nextElementSibling.id;
				return;
			} else if (playlistNumber === i 
				&& ul.children[j].nextElementSibling === null) {
				next[i] = ul.children[0].id;
			}
		}
	}
}

// https://stackoverflow.com/questions/5598743/finding-elements-position-relative-to-the-document
// Pour compenser le offsetLeft qui déconne.
function getCoords(elem) 
{
	var box = elem.getBoundingClientRect();
	var body = document.body;
	var docEl = document.documentElement;
	var scrollLeft = window.pageXOffset || docEl.scrollLeft || body.scrollLeft;
	var clientLeft = docEl.clientLeft || body.clientLeft || 0;
	var left = box.left + scrollLeft - clientLeft;
	return Math.round(left);
}

// Visuel barre :
var prog = document.getElementsByClassName("prog");
for (var i = 0; i <= audio.length - 1; i++) {
	prog[i].addEventListener("click", function(e)
	{
		position(e, this, "prog");
	});
}

// Gestion volume
var volumeF = document.getElementsByClassName("volumeF");
for (var i = 0; i <= audio.length - 1; i++) {
	volumeF[i].addEventListener("click", function(e)
	{
		position(e, this, "volume");
	});
}

function position(e, thisElement, localisation) {
	var xClient = e.clientX; // Position du curseur sur l'axe X (référenciel : gauche de la page).
	var leftOfDiv = getCoords(thisElement); // Position du début de l'élément sur l'axe X (référenciel : gauche de la div).
	var xPositionPointer = xClient - leftOfDiv; // Calul de la position du curseur par rapport à la gauche de la div.
	var width = thisElement.clientWidth; // Récupération de la width (sans les px pour le calcul).
	var purcent = 100 * xPositionPointer / width; // Mise en pourcentage de la position demandée.
	var audioParent = thisElement.parentNode.previousElementSibling;
	if (localisation === "volume") {
		audioParent.volume = purcent / 100;
		var widthProg = audioParent.volume * 100;
	} else if (localisation === "prog") {
		audioParent.currentTime = purcent * audioParent.duration / 100; // Met le currentTime à la position demandée.
		var widthProg = audioParent.currentTime * 100 / audioParent.duration;
	}
	thisElement.children[0].style.width = widthProg+"%";
}

// Préparation de la playlist
var playlist = document.getElementsByClassName("playlist");
var playerPlaylist = Array();
var next = Array();
for (var z = 0; z <= playlist.length - 1; z++) {
	var UlPlaylist = playlist[z].getElementsByTagName("ul")[0];
	playerPlaylist[z] = playlist[z].getElementsByTagName("audio")[0];
	playerPlaylist[z].src = UlPlaylist.children[0].id;
	UlPlaylist.children[0].classList.add("active");
	playerPlaylist[z].nextElementSibling.getElementsByClassName("title")[0].innerHTML = UlPlaylist.children[0].innerHTML;
	next[z] = UlPlaylist.children[1].id;
	forward[z].id = next[z];
	backward[z].id = UlPlaylist.lastElementChild.id;
	for (var j = 0; j <= UlPlaylist.childElementCount - 1; j++) {
		// Clic sur la liste
		UlPlaylist.children[j].addEventListener("click", function()
		{
			clearInterval(time);
			clearInterval(interval);
			// Bug connu des boucles contenant une fonction; z a été réincrémenté en entrant dedans.
			var playlistNow = this.parentNode.parentNode;
			for (var a = 0; a <= playlist.length - 1; a++) {
				if (playlistNow === playlist[a]) {
					z = a;
				}
			}
			playerPlaylist[z] = this.parentNode.parentNode.getElementsByTagName("audio")[0];
			playerPlaylist[z].src = this.id;
			playerPlaylist[z].nextElementSibling.getElementsByClassName("title")[0].innerHTML = this.innerHTML;
			ForwardAndBackward(z, this);
		});
	}

	// Backward clic
	playlist[z].getElementsByClassName("backward")[0].addEventListener("click", function()
	{
		clearInterval(time);
		clearInterval(interval);
		// Même chose
		var playlistNow = this.parentNode.parentNode;
		for (var a = 0; a <= playlist.length - 1; a++) {
			if (playlistNow === playlist[a]) {
				z = a;
			}
		}
		// Redéfinir UlPlaylist qui s'est perdu dans les bois
		UlPlaylist = playlist[z].getElementsByTagName("ul")[0];
		for (var b = 0; b <= UlPlaylist.childElementCount - 1; b++) {
			if (this.id === UlPlaylist.children[b].id) {
				var thisNow = UlPlaylist.children[b];
				var titleNow = UlPlaylist.children[b].innerHTML;
			}
		}
		playerPlaylist[z] = this.parentNode.parentNode.getElementsByTagName("audio")[0];
		playerPlaylist[z].src = backward[z].id;
		playerPlaylist[z].nextElementSibling.getElementsByClassName("title")[0].innerHTML = titleNow;
		ForwardAndBackward(z, thisNow);
	});

	// Forward clic
	playlist[z].getElementsByClassName("forward")[0].addEventListener("click", function()
	{
		clearInterval(time);
		clearInterval(interval);
		var playlistNow = this.parentNode.parentNode;
		for (var a = 0; a <= playlist.length - 1; a++) {
			if (playlistNow === playlist[a]) {
				z = a;
			}
		}
		UlPlaylist = playlist[z].getElementsByTagName("ul")[0];
		for (var b = 0; b <= UlPlaylist.childElementCount - 1; b++) {
			if (this.id === UlPlaylist.children[b].id) {
				var thisNow = UlPlaylist.children[b];
				var titleNow = UlPlaylist.children[b].innerHTML;
			}
		}
		playerPlaylist[z] = this.parentNode.parentNode.getElementsByTagName("audio")[0];
		playerPlaylist[z].src = forward[z].id;
		playerPlaylist[z].nextElementSibling.getElementsByClassName("title")[0].innerHTML = titleNow;
		ForwardAndBackward(z, thisNow);
	});
}

function ForwardAndBackward(z, thisNow)
{
	var childrens = thisNow.parentNode.children;
	for(var a = 0; a <= childrens.length - 1; a++) {
		childrens[a].classList.remove("active");
	}
	thisNow.classList.add("active");
	if (thisNow.nextElementSibling !== null) {
		next[z] = thisNow.nextElementSibling.id;
		forward[z].id = next[z];
	} else {
		next[z] = thisNow.parentNode.children[0].id;
		forward[z].id = next[z];
	}
	if (thisNow.previousElementSibling !== null) {
		backward[z].id = thisNow.previousElementSibling.id;
	} else {
		backward[z].id = thisNow.parentNode.lastElementChild.id;
	}
	for (var k = 0; k <= audio.length - 1; k++) {
		audio[k].pause();
		audio[k].nextElementSibling.getElementsByClassName("play")[0].style.display = null;
		audio[k].nextElementSibling.getElementsByClassName("pause")[0].style.display = "none";
	}
	playerPlaylist[z].nextElementSibling.getElementsByClassName("play")[0].style.display = "none";
	playerPlaylist[z].nextElementSibling.getElementsByClassName("pause")[0].style.display = null;
	playerPlaylist[z].play();
	Timer(false, true);
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console