css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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="metadata" 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="metadata" 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 3.0 License</a></p>
            
          
!
            
              "use strict";

// https://web.silvercherry.fr/player.html
// Version 18.43.1 (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="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 .progBarre,.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:0 5px;background-color:#EEE;border-style:solid;border-width:2px 0;border-color:#333;border-radius:5px}.player .pause span,.player .play span{display:inline-block;height:20px;border-style:solid;width:15px}.player .play span{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{display:flex;align-items:center;width:15px;height:30px;margin:5px;cursor:pointer}.player .play:hover span{border-color:transparent transparent transparent #666}.player .pause:hover span{border-color:transparent #666}.player .prog{overflow:hidden;cursor:pointer;width:100%;height:20px;margin:0 10px;background-color:#CCC}.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:100px;height:10px;margin:0 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:#EEE;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:#fff}';
head.appendChild(style);
var play = document.getElementsByClassName("play");
var pause = document.getElementsByClassName("pause");

// Mise en place du player (Et on profite de cette boucle pour check les titles et data-volume)
for (var i = 0; i <= audio.length - 1; i++) {
	audio[i].insertAdjacentHTML("afterend", player);
	if(audio[i].title !== null && audio[i].parentNode.className !== 'playlist') {
		audio[i].nextElementSibling.children[2].children[0].innerHTML = audio[i].title;
	} 
	if (audio[i].getAttribute("data-volume") && audio[i].getAttribute("data-volume")>=0 && audio[i].getAttribute("data-volume")<=1) {
		audio[i].volume = audio[i].getAttribute('data-volume');
		audio[i].nextElementSibling.children[4].children[0].style.width = (audio[i].getAttribute('data-volume')*100)+'%';
	}
}

// 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.children[0].style.display = 'inherit';
			audio[j].nextElementSibling.children[1].style.display = 'none';
		}
		this.style.display = 'none';
		this.nextElementSibling.style.display = 'inherit';
		Timer(true);
		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();
		Timer(true);
		this.style.display = 'none';
		this.previousElementSibling.style.display = 'inherit';
	});
}

// Fonction timer :
var time;
var timer = document.getElementsByClassName("timer");
var interval = setInterval(function()
{
	for (var i = 0; i <= audio.length - 1; i++) {
		var duree = timer[i].parentNode.previousElementSibling.duration;
		if (isNaN(duree)) {
			timer[i].innerHTML = '--:--';
		} else {
			var minutes = Math.trunc(duree.toFixed(0) / 60);
			var secondes = Math.trunc(duree % 60);
			minutes = ('0'+minutes).slice(-2);
			secondes = ('0'+secondes).slice(-2);
			timer[i].innerHTML = minutes+':'+secondes;
		}
	}
}, 2000);
setTimeout(function()
{
	clearInterval(interval);
}, 10100);
var progBarre = document.getElementsByClassName("progBarre");

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++) {
				var audiotime = audio[i].currentTime;
				var minutes = Math.trunc(audiotime.toFixed(0) / 60);
				var secondes = Math.trunc(audiotime % 60);
				minutes = ('0'+minutes).slice(-2);
				secondes = ('0'+secondes).slice(-2);
				if (audiotime !== 0) {
					timer[i].innerHTML = minutes+':'+secondes;
				} 
				if (isNaN(audio[i].duration)) {
					timer[i].innerHTML = '--:--';
				}
				var widthProg = audio[i].currentTime / audio[i].duration * 100;
				progBarre[i].style.width = widthProg+'%';
				if (audiotime === audio[i].duration && activatePlaylist === false && !audio[i].hasAttribute("loop")) {
					audio[i].currentTime = 0;
					audio[i].pause();
					progBarre[i].style.width = '0%';
					timer[i].innerHTML = '00:00';
					audio[i].nextElementSibling.children[0].style.display = 'inherit';
					audio[i].nextElementSibling.children[1].style.display = 'none';
					Timer(true);
				} else if (audiotime === audio[i].duration && activatePlaylist === true) {
					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.children[2].children.length - 1; j++) {
								if (thisPlaylist.children[2].children[j].nextElementSibling !== null 
									&& thisPlaylist.children[2].children[j].nextElementSibling.id === next[k]) {
									playerPlaylist[k].nextElementSibling.children[2].children[0].innerHTML = thisPlaylist.children[2].children[j].nextElementSibling.innerHTML;
								} else if (thisPlaylist.children[2].children[j].nextElementSibling === null 
									&& thisPlaylist.children[2].children[0].id === next[k]) {
									playerPlaylist[k].nextElementSibling.children[2].children[0].innerHTML = thisPlaylist.children[2].children[0].innerHTML;
								}
							}
						}
					}
					audio[i].play();
					Next(nextPlaylistNumber);
				}
			}
		}, 500);
	}
}

// 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)
	{
		var xClient = e.clientX; // Position du curseur sur l'axe X (référenciel : gauche de la page).
		var leftOfDiv = getCoords(this); // 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 = this.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 = this.parentNode.previousElementSibling;
		audioParent.currentTime = purcent * audioParent.duration / 100; // Met le currentTime à la position demandée.
		var widthProg = audioParent.currentTime * 100 / audioParent.duration;
		this.children[0].style.width = widthProg+'%';
	});
}

// Gestion volume
var volumeF = document.getElementsByClassName("volumeF");
for (var i = 0; i <= audio.length - 1; i++) {
	volumeF[i].addEventListener("click", function(e)
	{
		var xClient = e.clientX;
		var leftOfDiv = getCoords(this);
		var xPositionPointer = xClient - leftOfDiv;
		var width = this.clientWidth;
		var purcent = 100 * xPositionPointer / width;
		var audioParent = this.parentNode.previousElementSibling;
		audioParent.volume = purcent / 100;
		var widthProg = audioParent.volume * 100;
		this.children[0].style.width = widthProg+'%';
	});
}

// Préparation de la playlist
var playlist = document.getElementsByClassName("playlist");
var tempo;
var playerPlaylist = Array();
var next = Array();
for (var z = 0; z <= playlist.length - 1; z++) {
	playerPlaylist[z] = playlist[z].children[0];
	playerPlaylist[z].src = playlist[z].children[2].children[0].id;
	playerPlaylist[z].nextElementSibling.children[2].children[0].innerHTML = playlist[z].children[2].children[0].innerHTML;
	next[z] = playlist[z].children[2].children[1].id;
	for (var j = 0; j <= playlist[z].children[2].childElementCount - 1; j++) {
		playlist[z].children[2].children[j].addEventListener("click", function()
		{
			clearInterval(interval);
			// Bug connu des boucles ; z a été réincrémenté. J'ai pas encore étudié pourquoi ce comportement.
			for (var a = 0; a <= playlist.length - 1; a++) {
				for (var b = 0; b <= playlist[a].children[2].childElementCount - 1; b++) {
					if (this.id === playlist[a].children[2].children[b].id) {
						z = a;
					}
				}
			}
			playerPlaylist[z] = this.parentNode.parentNode.children[0];
			playerPlaylist[z].src = this.id;
			playerPlaylist[z].nextElementSibling.children[2].children[0].innerHTML = this.innerHTML;
			if (this.nextElementSibling !== null) {
				next[z] = this.nextElementSibling.id;
			} else {
				next[z] = this.parentNode.children[0].id;
			}
			for (var k = 0; k <= audio.length - 1; k++) {
				audio[k].pause();
				audio[k].nextElementSibling.children[0].style.display = 'inherit';
				audio[k].nextElementSibling.children[1].style.display = 'none';
			}
			playerPlaylist[z].nextElementSibling.children[0].style.display = 'none';
			playerPlaylist[z].nextElementSibling.children[1].style.display = 'inherit';
			playerPlaylist[z].play();
			Timer(false, true);
		});
	}
}

function Next(playlistNumber)
{
	for (var i = 0; i <= playlist.length - 1; i++) {
		for (var j = 0; j <= playlist[i].children[2].childElementCount - 1; j++) {
			if (playlistNumber === i 
				&& playlist[i].children[2].children[j].id === next[i] 
				&& playlist[i].children[2].children[j].nextElementSibling !== null) {
				next[i] = playlist[i].children[2].children[j].nextElementSibling.id;
				return;
			} else if (playlistNumber === i 
				&& playlist[i].children[2].children[j].nextElementSibling === null) {
				next[i] = playlist[i].children[2].children[0].id;
			}
		}
	}
}
            
          
!
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.
Loading ..................

Console