Using local file as <audio> src
I can think of two ways.
Within your audio tag:
src="file:///C:/.../file.mp3"
or you could use a Blob using the file API.
HTML:
<input type="file"></input>
JS:
audio.src = URL.createObjectURL(document.getElementsByTagName('input')[0].files[0]);
![Rob Allsopp](https://i.stack.imgur.com/fS8az.jpg?s=256&g=1)
Rob Allsopp
I love to code! I am perfectly at home working out front in React or Angular, or behind the scenes in Node or Go. I have also been known to dabble in machine learning and digital signal processing from time to time. I have always been a huge science/space nerd, and recently I have taken a slight detour from web development and begun pursuing a master's degree in scientific computing. I hope to eventually write software in the aerospace industry.
Updated on July 24, 2022Comments
-
Rob Allsopp almost 2 years
Is it possible to use an audio file from the user's hard drive as the
src
attribute for an HTML5<audio>
tag? Maybe through an<input type="file" />
? This might not be particularly useful in production, but I'm still curious if it can be done. -
Rob Allsopp over 10 yearsnice, the file API method worked like a charm for me!
-
Paul Martin over 10 yearsalso of note. If you want to change the audio tag every time the file changes, there is an 'change' event for the input tag. that will fire every time you change the file in the input tag.
-
Alex McMillan almost 9 yearsI just get
Not allowed to load local resource
. Stupid damn security. Surely feeding an.mp3
directly into thesrc
of anaudio
element should be supported? -
Marcel about 3 years@AlexMcMillan It should not. Or else, a random script your browser loads, could play through your music collection at once.