admin 管理员组

文章数量: 1086019

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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

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();
});

本文标签: htmlHow can I play sound in JavaScript without using the ltaudiogt tagStack Overflow