- Joined
- Dec 14, 2021
- Messages
- 28
- Reaction score
- 2
I have this audio player I am working on and well I can't get it to work, since the audio tag needs to have the full file path to play the audio file, so I need a way to play the audio file, regardless of the location of the audio file, so how would I do it?
Here is the code:
Here is the HTML:
Here is the code:
JavaScript:
function getHHMMSSFromSeconds(totalSeconds) {
if (!totalSeconds) {
return '00:00:00';
}
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor(totalSeconds % 3600 / 60);
const seconds = totalSeconds % 60;
const hhmmss = padTo2(hours) + ':' + padTo2(minutes) + ':' + padTo2(seconds);
return hhmmss;
}
var trackHold = document.getElementById("allTracks");
var trackFile = document.getElementById("trackFile");
var trackAddModal = document.getElementById("trackAddModal");
var addTrck = document.getElementById("addTrck");
var span = document.getElementsByClassName("close")[0];
addTrck.onclick = function() {
trackAddModal.style.display = "block";
}
span.onclick = function() {
trackAddModal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == trackAddModal) {
trackAddModal.style.display = "none";
}
}
var trackNum = 0;
document.getElementById("trackNum").innerHTML = trackNum;
function addTrack(tkName, tkLength) {
var file = trackFile.files[0];
var trackSource = file.name;
if (tkName == null) {
tkName = trackSource;
}
var trackName = tkName;
var audio = document.createElement('audio');
audio.src = trackFile.value;
if (tkLength == null) {
tkLength = getHHMMSSFromSeconds(parseInt(audio.duration));
}
var trackLth = tkLength;
trackHold.innerHTML += "<div class='track'><button class='playTrack'>Play</button> | <button class='removeTrack'>Remove</button><audio controls><source src="+"'"+trackSource+"'"+">Your browser does not support the audio element.</audio><br><br><div class='ttrrds'><p class='trackName'>Name of Track: "+trackName+"</p><br><p class='trackLength'>"+trackLth+"</p></div><p>Track Number: <span style='font-weight:bold;'>"+trackNum+"</span></p></div><br>";
trackNum += 1;
document.getElementById("trackNum").innerHTML = trackNum;
}
Here is the HTML:
HTML:
| <button id="addTrck">Add Track</button> |
<p style="display:inline;">Number of Tracks: <span id="trackNum"></span></p>
<hr>
<div class="marg" id="allTracks"></div>
<br>
<div id="trackAddModal" class="modal">
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>Add a Track</h2>
</div>
<div class="modal-body marg">
<label for="trackFile">Select a file: (Only Audio Files)</label>
<input type="file" id="trackFile" name="trackFile" accept="audio/*">
<br><br>
<button onclick="addTrack()">Submit</button>
</div>
</div>
</div>