How can play sound using JavaScript only, without 开发者_JAVA百科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?
No 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();
});
精彩评论