I'm trying to implement a music player using javascript, which works by uploading an audio file (.mp3), in the script when uploading the file, which occurs with the 'onload' event the function created has the variable 'files' and then play the file. How does the file get played if it does not seem to reference the variable that receives the file?
HTML
<div id="content">
<input type="file" id="thefile" accept="audio/*" />
<audio id="audio" controls="controls"></audio>
</div>
Javascript
window.onload = function() {
var file = document.getElementById("thefile");
var audio = document.getElementById("audio");
file.onchange = function() {
var files = this.files;
audio.src = URL.createObjectURL(files[0]);
audio.load();
audio.play();
}
};
Thank you in advance.