最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How can I play sound in JavaScript, without using the <audio> tag? - Stack Overflow

programmeradmin8浏览0评论

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();
});
发布评论

评论列表(0)

  1. 暂无评论