Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript Audio object vs. HTML5 Audio tag

In a project recently when I loaded a sound with

var myAudio = new Audio("myAudio.mp3"); myAudio.play(); 

It played fine unless a dialogue was opened (ie alert, confirm). However when I instead tried adding an audio tag in my html

<audio id="audio1">     <source src="alarm.mp3" type="audio/mpeg" /> </audio> 

and using

var myAudio1 = document.getElementById("audio1"); myAudio1.play() 

it continued to play after a dialogue was opened. Does anyone know why this is? Also more generally what are the differences between the two ways to play sounds?

like image 932
ekcrisp Avatar asked Jan 30 '14 17:01

ekcrisp


People also ask

Is HTML5 an audio tag?

Since the release of HTML5, audios can be added to webpages using the “audio” tag.

What is audio object in JavaScript?

The Audio object interface exposes properties, methods and events that can be used to program audio software and sound related programs using JavaScript. var audio = new Audio(); audio. src = "file_name.

Is audio tag deprecated?

The <bgsound> HTML element is deprecated. It sets up a sound file to play in the background while the page is used; use <audio> instead. Warning: Do not use this!

Can you style HTML5 audio tags?

HTML 5 audio tags can be styled. By using the audio tag with “controls” attribute, the default browsers player is used. You can customize by not using the browsers controls. You can also add CSS classes to each one of the elements and style them accordingly.


2 Answers

According to this wiki entry at Mozilla <audio> and new Audio() should be the same but it doesn't look like that is the case in practice. Whenever I need to create an audio object in JavaScript I actually just create an <audio> element like this:

var audio = document.createElement('audio'); 

That actually creates an audio element that you can use exactly like an <audio> element that was declared in the page's HTML.

To recreate your example with this technique you'd do this:

var audio = document.createElement('audio'); audio.src = 'alarm.mp3' audio.play(); 
like image 195
pseudosavant Avatar answered Sep 20 '22 14:09

pseudosavant


JavaScript halts during an Alert or Confirm box.

You cannot concurrently run code and display an alert(), confirm(), or prompt(), it literally waits for user input on this, this is a core feature of JavaScript.

I am assuming it is that very reason why an audio file played entirely within JavaScript scope does this. Comparatively Flash video clips or HTML5 audio/video will continue to play on even when a JavaScript alert/confirm/prompt is open.

As for what method is better, well that is up to you. It is pretty archaic to do anything with the JavaScript built in alert/confirm/prompt anymore, there are way better looking prompts you can make with jQuery UI and so on.

If you have a lot of dynamic content on the page or are you looking into background buffering audio before they need to be triggered and so on, then JavaScript is probably the saner way to go about things.

If you have literally just one player on the screen then there is no excuse for not putting in onto the HTML code. Although unlikely to affect anyone these days, it is still bad practice to rely heavily on JavaScript when there is no reason to.

like image 24
Aaron Avatar answered Sep 22 '22 14:09

Aaron