Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using local file as <audio> src

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.

like image 945
Rob Allsopp Avatar asked Feb 12 '14 19:02

Rob Allsopp


People also ask

What is src in audio tag?

The src attribute specifies the location (URL) of the audio file.

Can we embed an audio file with source tag?

The <source> element allows you to specify alternative audio files which the browser may choose from. The browser will use the first recognized format. The text between the <audio> and </audio> tags will only be displayed in browsers that do not support the <audio> element.

How do I make an audio file a link in HTML?

Linking to a sound file using a href allows a browser to open and play an audio file if the viewer of your web page has properly configured their Internet browser. You can also use the <embed> tag or the newer <audio> tag to insert a sound file directly into a web page.

How do I change the audio src in HTML?

You can change the audio file of the HTML5 player with just one line of JavaScript code that you can see below: document. getElementById("my-audio"). setAttribute('src', 'AUDIO_SRC_FILE');


1 Answers

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]);
like image 172
Paul Martin Avatar answered Sep 19 '22 01:09

Paul Martin