Using local file as <audio> src

15,924

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]);
Share:
15,924
Rob Allsopp
Author by

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, 2022

Comments

  • Rob Allsopp
    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
    Rob Allsopp over 10 years
    nice, the file API method worked like a charm for me!
  • Paul Martin
    Paul Martin over 10 years
    also 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
    Alex McMillan almost 9 years
    I just get Not allowed to load local resource. Stupid damn security. Surely feeding an .mp3 directly into the src of an audio element should be supported?
  • Marcel
    Marcel about 3 years
    @AlexMcMillan It should not. Or else, a random script your browser loads, could play through your music collection at once.