How can I add multiple sources to an HTML5 audio tag, programmatically?

Why add multiple files with JavaScript when you can just detect the types supported? I would suggest instead detecting the best type then just setting the src. var source= document.createElement(‘source’); if (audio.canPlayType(‘audio/mpeg;’)) { source.type=”audio/mpeg”; source.src=”https://stackoverflow.com/questions/4053262/audio/song.mp3″; } else { source.type=”audio/ogg”; source.src=”https://stackoverflow.com/questions/4053262/audio/song.ogg”; } audio.appendChild(source); Add as many checks as you have file types.

Saving WAV File Recorded in Chrome to Server

Client side JavaScript function to upload the WAV blob: function upload(blob) { var xhr=new XMLHttpRequest(); xhr.onload=function(e) { if(this.readyState === 4) { console.log(“Server returned: “,e.target.responseText); } }; var fd=new FormData(); fd.append(“that_random_filename.wav”,blob); xhr.open(“POST”,”<url>”,true); xhr.send(fd); } PHP file upload_wav.php: <?php // get the temporary name that PHP gave to the uploaded file $tmp_filename=$_FILES[“that_random_filename.wav”][“tmp_name”]; // rename the temporary file … Read more

switch audio source with jquery and HTML5 audio tag

Your change function should be like this: function change(sourceUrl) { var audio = $(“#player”); $(“#ogg_src”).attr(“src”, sourceUrl); /****************/ audio[0].pause(); audio[0].load();//suspends and restores all audio element //audio[0].play(); changed based on Sprachprofi’s comment below audio[0].oncanplaythrough = audio[0].play(); /****************/ } The problems were audio.empty() and the audio var. You were attempting to append an emptied audio tag, and didn’t … Read more

How do you check if a HTML5 audio element is loaded?

To find out when the audio is ready to start playing, add listeners for the oncanplay or oncanplaythrough events. To find out when the audio has loaded at all, listen to the onloadeddata event: <audio oncanplay=”myOnCanPlayFunction()” oncanplaythrough=”myOnCanPlayThroughFunction()” onloadeddata=”myOnLoadedData()” src=”https://stackoverflow.com/questions/8059434/myaudio.ogg” controls> <a href=”https://stackoverflow.com/questions/8059434/myaudio.ogg”>Download</a> </audio> <script> function myOnCanPlayFunction() { console.log(‘Can play’); } function myOnCanPlayThroughFunction() { console.log(‘Can play … Read more

html5 display audio currentTime

Here’s an example: <audio id=”track” src=”http://upload.wikimedia.org/wikipedia/commons/a/a9/Tromboon-sample.ogg” ontimeupdate=”document.getElementById(‘tracktime’).innerHTML = Math.floor(this.currentTime) + “https://stackoverflow.com/” + Math.floor(this.duration);”> <p>Your browser does not support the audio element</p> </audio> <span id=”tracktime”>0 / 0</span> <button onclick=”document.getElementById(‘track’).play();”>Play</button> This should work in Firefox and Chrome, for other browsers you’ll probably need to add alternative encodings.

How to play a notification sound on websites?

2021 solution function playSound(url) { const audio = new Audio(url); audio.play(); } <button onclick=”playSound(‘https://your-file.mp3’);”>Play</button> Browser support Edge 12+, Firefox 20+, Internet Explorer 9+, Opera 15+, Safari 4+, Chrome Codecs Support Just use MP3 Old solution (for legacy browsers) function playSound(filename){ var mp3Source=”<source src=”” + filename + ‘.mp3″ type=”audio/mpeg”>’; var oggSource=”<source src=”” + filename + ‘.ogg” … Read more

change src with javascript

Try this snippet list.onclick = function(e) { e.preventDefault(); var elm = e.target; var audio = document.getElementById(‘audio’); var source = document.getElementById(‘audioSource’); source.src = elm.getAttribute(‘data-value’); audio.load(); //call this to just preload the audio without playing audio.play(); //call this to play the song right away }; <ul style=”list-style: none”> <li>Audio Files <ul id=”list”> <li><a href=”#” data-value=”http://media.w3.org/2010/07/bunny/04-Death_Becomes_Fur.oga”>Death_Becomes_Fur.oga</a></li> <li><a href=”#” … Read more

HTML5 Audio Looping

While loop is specified, it is not implemented in any browser I am aware of Firefox [thanks Anurag for pointing this out]. Here is an alternate way of looping that should work in HTML5 capable browsers: var myAudio = new Audio(“https://stackoverflow.com/questions/3273552/someSound.ogg”); myAudio.addEventListener(‘ended’, function() { this.currentTime = 0; this.play(); }, false); myAudio.play();

How can I add predefined length to audio recorded from MediaRecorder in Chrome?

This is a chrome bug. FF does expose the duration of the recorded media, and if you do set the currentTimeof the recorded media to more than its actual duration, then the property is available in chrome… var recorder, chunks = [], ctx = new AudioContext(), aud = document.getElementById(‘aud’); function exportAudio() { var blob = … Read more