How can play sound using JavaScript only, without using the <audio>
tag?
I'm using:
var audio = {};
audio["walk"] = new Audio();
audio["walk"].src = "Scr/dave.wma"
audio["walk"].load()
setTimeout('audio["walk"].play()',1000);
But it doesn’t play?
How can play sound using JavaScript only, without using the <audio>
tag?
I'm using:
var audio = {};
audio["walk"] = new Audio();
audio["walk"].src = "Scr/dave.wma"
audio["walk"].load()
setTimeout('audio["walk"].play()',1000);
But it doesn’t play?
Share Improve this question edited Aug 19, 2011 at 18:32 Paul D. Waite 99k57 gold badges203 silver badges271 bronze badges asked Aug 19, 2011 at 1:20 Moh97Moh97 732 silver badges8 bronze badges2 Answers
Reset to default 6No browsers support the Windows Media Audio format.
You should be using the Ogg Vorbis, MP3 and Wave formats.
Here is a table of audio format support in the five major browsers:
Ogg Vorbis MP3 Wave
Firefox * *
Safari * *
Chrome * *
Opera * *
Internet Explorer * *
Please note: the above table may be outdated.
In addition, as alex has stated, the statements are asynchronous and do not wait for the audio file to have loaded. You will need to listen for the load
event and call play
within the event handler.
As Delan says, no browser supports WMA.
However, your code still has a problem.
You need to wait until the audio
has loaded.
var audio = {};
audio["walk"] = new Audio();
audio["walk"].src = "Scr/dave.wma"
audio["walk"].addEventListener('load', function() {
audio["walk"].play();
});