用Javascript性能播放声音是否繁重? [英] Is playing sound in Javascript performance heavy?

查看:78
本文介绍了用Javascript性能播放声音是否繁重?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Javascript做一个简单的游戏,其中当一个物体与墙壁碰撞时,它会发出"的声音.声音的响度取决于物体的速度(更高的速度=>更大的声音).

I'm making a simple game in Javascript, in which when an object collides with a wall, it plays a "thud" sound. That sound's loudness depends on the object's velocity (higher velocity => louder sound).

播放功能:

playSound = function(id, vol) //ID of the sound in the sounds array, volume/loudness of the sound
{
    if (vol) //sometimes, I just want to play the sound without worrying about volume
        sounds[id].volume = vol;
    else
        sounds[id].volume = 1;

    sounds[id].play();
}

我怎么称呼:

playSound(2, Math.sqrt(p.vx*p.vx + p.vy*p.vy)/self.TV); //self.TV stands for terminal velocity. This calculates the actual speed using the basic Pythagora's theorem and then divides it by self.TV, which results in a number from 0 to self.TV. 2 is the id of the sound I want to play.

在Chrome中,一切正常.但是,在Firefox中,每次与墙发生碰撞(=> playSound被调用)时,都会有一个暂停持续将近半秒钟!起初,我认为问题出在Math.sqrt,但是我错了.这就是我测试的方式:

In Chrome, things work quite well. In Firefox, though, each time a collision with a wall happens (=> playSound gets called), there's a pause lasting almost half a second! At first, I thought that the issues were at Math.sqrt, but I was wrong. This is how I tested it:

//playSound(2, 1); //2 Is the id of the sound I want to play, and 1 is max loudness
Math.sqrt(p.vx*p.vx + p.vy*p.vy)/self.TV;
Math.sqrt(p.vx*p.vx + p.vy*p.vy)/self.TV;
Math.sqrt(p.vx*p.vx + p.vy*p.vy)/self.TV;

这完全消除了碰撞滞后,使我相信Math.sqrt根本不会引起任何问题.只是为了确定,我这样做了:

This completely removed the collision lag, and lead me to believe that Math.sqrt isn't causing any problems at all. Just to be sure, though, I did this:

playSound(2, 1); //2 Is the id of the sound I want to play, and 1 is max loudness
//Math.sqrt(p.vx*p.vx + p.vy*p.vy)/self.TV;
//Math.sqrt(p.vx*p.vx + p.vy*p.vy)/self.TV;
//Math.sqrt(p.vx*p.vx + p.vy*p.vy)/self.TV;

而滞后又回来了!现在,我确定播放声音会引起问题.我对么?为什么会这样呢?我该如何解决?

And the lag was back! Now I'm sure that playing a sound causes problems. Am I correct? Why is this happening? How do I fix it?

推荐答案

我遇到了同样的延迟问题,玩家在发射武器时会发出声音.我的解决方案有两个方面:

I ran into this same delay issue making a sound when the player fires a weapon. My solution was two-fold:

  1. 在加载时播放每种声音,然后立即将其暂停.这将使它能够快速恢复播放,而不是从头开始播放.每次播放声音后,请执行此播放暂停技术.

  1. Play each sound at load time and then pause it immediately. This will allow it to resume playing quickly, rather than playing from scratch. Do this play-pause technique after every play of the sound.

对每种声音使用<audio>对象池,而不对每种声音类型使用单个音频对象.不仅仅是使用sounds[id],还可以使用通过sound[id][count]访问的2D数组.在这里,sound[id]是所有具有相同声音的音频对象的列表,而count是用于该声音ID的当前对象的索引.每次调用playSound(id)时,增加与该ID相关的计数,以便下一个调用调用一个不同的音频对象.

Use a pool of <audio> objects for each sound, rather than a single audio object for each sound type. Instead of just using sounds[id], use a 2D array, accessed with sound[id][count]. Here, sound[id] is a list of audio objects that all have the same sound, and count is the index of current object in use for that sound id. With each call to playSound(id), increment the count associated with that id, so that the next call invokes a different audio object.

我不得不将它们一起使用,因为播放暂停技术可以很好地将缓冲延迟移到需要播放声音的 之前,但是如果您需要快速播放声音,则可以仍然会得到延误.这样,最近使用的音频对象可以在另一个对象播放时充电".

I had to use these together, because the play-pause technique does a good job of moving the buffering delay to before the sound needs to be played, but if you need the sound rapidly, you'll still get the delay. This way, the most recently used audio object can "recharge" while another object plays.

这篇关于用Javascript性能播放声音是否繁重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆