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

查看:42
本文介绍了如何设置 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();
}

sounds 是一个充满 new Audio("url") 的数组.

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

推荐答案

使用音频元素的音量属性.来自 W3:

Use the audio element's volume property. From W3:

元素的有效媒体音量是音量,解释为相对范围为 0.0 到 1.0,其中 0.0 表示无声,1.0 表示最响亮的设置,介于两者之间的值随着响度的增加而增加.范围不必是线性的.最响的设置可能低于系统的尽可能响亮的设置;例如,用户可以设置一个最大音量.

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.

例如:sounds[id].volume=.5;

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

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