So i am working on a school project and am pretty new to coding. My project is to make a little playlist with javascript, but I only want my project to have one universal button and have multiple audio sources. I want the user to be able to select the song and be able to play it with the button. Any ideas?
Asked
Active
Viewed 73 times
0
-
S.O. is more for trouble-shooting a problem in code. I suggest you post this question in something like r/javascript – Ju66ernaut Jun 05 '17 at 17:47
-
Possible duplicate of [switch audio source with jquery and HTML5 audio tag](https://stackoverflow.com/questions/9421505/switch-audio-source-with-jquery-and-html5-audio-tag) – Heretic Monkey Jun 05 '17 at 17:52
1 Answers
0
This uses HtmlAudioElement, I did not test with the audios, but that's the idea, may help you as a reference to implement your code.
var aux = 1;
function playAudiox() {
if (aux == 1) {
console.log("Playing Track 1.");
var track1 = new Audio('track1.mp3');
track1.play();
aux++;
} else if (aux == 2) {
console.log("Playing Track 2.");
var track2 = new Audio('track2.mp3');
track2.play();
aux++;
} else if (aux == 3) {
console.log("Playing Track 3.");
var track3 = new Audio('track3.mp3');
track3.play();
aux = 1;
}
}
#tracklist {
height: 200px;
height: 100px;
border: 2px solid black;
}
.tracks {
border: 1px solid red;
height: 100px;
width: 80px;
float: left;
}
<div id="tracklist">
<div class="tracks">track1</div>
<div class="tracks">track2</div>
<div class="tracks">track3</div>
</div><br>
<button id="universalPlay" onclick="playAudiox()">Universal Play</button>

Mathiasfc
- 1,627
- 1
- 16
- 24
-
thanks it plays the audio files in order but not are selectable. Thank you for your time. – jacky zeng Jun 07 '17 at 05:40
-
@jackyzeng it's simple to make it selectable, create one parameter in `playAudiox(track)` and on div click call e,g `playAudiox(divNumber)` – Mathiasfc Jun 07 '17 at 11:25