如何设置HTML5音频的音量? [英] How to set the loudness of HTML5 audio?

查看:1469
本文介绍了如何设置HTML5音频的音量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在HTML5游戏我做,我玩的是砰的声音时,事情发生碰撞。然而,这是有点不现实。不管对象的速度,它们将总是相同的,相对大声的砰声。我想要做的是有声音的响度取决于速度,但我怎么做呢?我只知道如何播放声音。

In an HTML5 game I'm making, I play a "thud" sound when things collide. However, it is a bit unrealistic. No matter the velocity of the objects, they will always make the same, relatively loud "thud" sound. What I'd like to do is to have that sound's loudness depend on velocity, but how do I do that? I only know how to play a sound.

playSound = function(id)
{
    sounds[id].play();
}

声音是一个充满新的音频(URL)的阵列的。

sounds is an array full of new Audio("url")'s.

推荐答案

使用音频元素的音量属性。从 W3 :元素的有效介质体积为体积,除$ P相对$ PTED至介于0.0到1.0,0.0是无语了,1.0是最响亮的设置,之间的值响度增大。范围不必是线性的。最响亮的设置可能比系统的最响亮的可能设置较低;例如,用户可以设置最大音量。

Use the audio element's volume property. From W3: The element's effective media volume is volume, interpreted relative to the range 0.0 to 1.0, with 0.0 being silent, and 1.0 being the loudest setting, values in between increasing in loudness. The range need not be linear. The loudest setting may be lower than the system's loudest possible setting; for example the user could have set a maximum volume.

例如:声音[ID] .volume = 0.5;

这篇关于如何设置HTML5音频的音量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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