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

html - Play a JavaScript sound file without a delay - Stack Overflow

programmeradmin0浏览0评论

I have a HTML5 canvas game with sounds, but one of the problems that I am running into is that when the sound is played, all other action stops until the sound has finished playing. I used this code

document.getElementById("music").innerHTML="<embed src=\""+surl+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />"


Where surl is the url of the sound and music of the span element that is playing the music. Does anyone know how to play a sound without delaying the entire program?


I tried it with a considerably larger file, and it turns out that while it DOES play in the background, there is still a considerable delay between starting the sound and continuing the game.

I have a HTML5 canvas game with sounds, but one of the problems that I am running into is that when the sound is played, all other action stops until the sound has finished playing. I used this code

document.getElementById("music").innerHTML="<embed src=\""+surl+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />"


Where surl is the url of the sound and music of the span element that is playing the music. Does anyone know how to play a sound without delaying the entire program?


I tried it with a considerably larger file, and it turns out that while it DOES play in the background, there is still a considerable delay between starting the sound and continuing the game.

Share Improve this question edited Aug 3, 2015 at 20:59 AstroCB 12.4k20 gold badges58 silver badges74 bronze badges asked Feb 26, 2013 at 0:09 scrblnrd3scrblnrd3 7,4169 gold badges36 silver badges65 bronze badges 1
  • You can try web worker:-) – geniuscarrier Commented Feb 26, 2013 at 0:17
Add a comment  | 

5 Answers 5

Reset to default 9

Try using the native HTML5 Audio API. First, create the instances of the sounds you will need:

var ping = new Audio("ping.ogg");

Note: You do not need to insert these audio instances into the DOM at any point.

When you're ready to play the sound, e.g. when someone clicks:

document.querySelector(".ping").addEventListener("click", function() {

    // ping clicked, play ping sound:
    ping.play()
})

Because the ping instance is pre-loaded there should be no delay in playing the sound, and you can play it as many times as you like.

Keep in mind that codec support is not consistent cross-browser, so you will have to have an ogg source and an MP3 source, or another combination (support tables can be found here http://en.wikipedia.org/wiki/HTML5_Audio#.3CAudio.3E_element_format_support).

If you want a more backwards-compatible approach I recommend SoundManager2 as a complete solution with an easy API: http://www.schillmania.com/projects/soundmanager2/

Otherwise, the documentation for the native HTML5 Audio API can be found here: https://developer.mozilla.org/en-US/docs/HTML/Element/audio and here https://developer.mozilla.org/en-US/docs/DOM/HTMLAudioElement

You are relying on whatever external plugin the user might have to play the audio by using embed, so the performance might vary wildly from user to user.

My recommendation would be to use a library like howler.js, which was built with games in mind. HTML5 audio can be quite the pain to get right, especially for games, so you're going to save yourself from a lot of headache by using a library instead of trying to fiddle with HTML5 audio elements yourself.

I've had good success using this framework: http://www.schillmania.com/projects/soundmanager2/

It has decent cross browser support, and you can preload/start/stop sounds with JavaScript. I've used it with sounds playing while animations are occurring and didn't see any delay. You can also have it wait for all your sound assets to be loaded before doing something, and in the meantime show a loading screen or something.

Web Audio API is right tool for this job. There are plenty of libraries available that simplifies using the little complex API. You can also checkout musquito a tiny library created by me that manages the complexity in Web Audio API and helps to create and manage single or group of sounds.

I did a bit of digging around on some other stackoverflow posts, and I found that in HTML5, you can just do

var sound=new Audio("hello.wav");

and then call

sound.play();

That seems to do the trick, and there is no delay.

发布评论

评论列表(0)

  1. 暂无评论