本页面仅提供有关 WebGL 中的音频功能的信息。要了解如何在 Unity 项目中使用音频,请参阅音频概述。
Unity WebGL has limited audio functionality and only supports the basic features, because Unity usually uses FMOD to manage audio for platforms. However, FMOD relies on threads, which WebGL doesn’t support. For this reason, Unity uses an implementation based on the internal Web Audio API, which enables the browser to handle audio playback and mixing.
Note: Google Chrome’s new Autoplay policy prevents autoplay of audio and video under certain conditions. For example, while your game might be set to autoplay some background music soon after the game loads, it won’t play automatically unless you click or tap on the website. For more information on how to enable or disable this policy, see Google Chrome’s documentation on Autoplay policy in Chrome.
Unity WebGL 支持以下 API 类:
类 | WebGL 支持状态 |
---|---|
AudioSource | WebGL 支持部分 API。有关特定支持详细信息,请参阅 AudioSource。 |
AudioListener | 支持所有 API。 |
AudioClip | WebGL 支持部分 API。有关特定支持详细信息,请参阅 AudioClip。 |
SystemInfo.supportsAudio | 浏览器为 WebGL 提供音频支持。因此,SystemInfo.supportsAudio 始终为 true。 |
Microphone | 不支持。 |
AudioSource API 支持基本位置音频回放,包括:
Unity WebGL 支持以下 AudioSource API:
属性 | 总结 |
---|---|
Clip | 确定下一个播放的音频剪辑。 |
dopplerLevel | 设置该 AudioSource 的多普勒缩放。 |
ignoreListenerPause | 运行 AudioSource 忽略 AudioListener.pause 并继续播放音频。 |
ignoreListenerVolume | 忽略终端用户的 AudioSource 音量。 |
isPlaying | 如果 AudioSource.clip 正在播放,则返回 true。 |
loop | 允许应用程序循环播放 AudioSource.clip 。 |
maxDistance | 设置 AudioSource.clip 停止衰减或听不到的最大距离。 |
minDistance | 设置 AudioSource.clip 音量不再增大的最小距离。超过最小距离,音量开始衰减。 |
mute | AudioSource 静音。 |
pitch | 设置 AudioSource.clip 的音高。WebGL 仅支持正音高值。 |
playOnAwake | 唤醒时播放 AudioSource。 |
rolloffMode | 设置 AudioSource 随距离衰减。 |
time | 设置播放位置(以秒为单位)。 |
timeSamples | 设置脉冲编码调制 (PCM) 样本中的播放位置。 |
velocityUpdateMode | 设置以固定还是动态循环方式更新 AudioSource。 |
volume | 设置 AudioSource 的音量(0.0 到 1.0)。 |
方法 | 总结 |
---|---|
Pause | 暂停 AudioSource.clip 。 |
Play | 播放 AudioSource.clip 。 |
PlayDelayed | 以指定的延迟播放 AudioSource.clip (以秒为单位)。 |
PlayOneShot | 播放 AudioClip,并根据 volumeScale 调整 AudioSource 的音量。 |
PlayScheduled | 在指定的时间播放 AudioSource。 |
SetScheduledEndTime | 设置计划的 AudioSource.clip 结束的时间。 |
SetScheduledStartTime | 设置计划的 AudioSource.clip 开始的时间。 |
Stop | 停止播放 AudioSource.clip 。 |
UnPause | 解除暂停的 AudioSource.clip 。 |
PlayClipAtPoint | 在世界空间中的给定位置播放 AudioSource.clip 。 |
Unity WebGL 以 AAC 格式导入 AudioClip 文件,这是浏览器广泛支持的格式。Unity WebGL 支持以下 AudioClip API:
属性 | 总结 |
---|---|
length | AudioClip 的长度(以秒为单位)。 |
loadState | 返回与 AudioClip 关联的音频数据的当前加载状态。 |
samples | 样本中 AudioClip 的长度。 |
loadType | 剪辑的加载类型。您可以在 Inspector 中设置 AudioClip 的加载类型。 |
方法 | 总结 | 其他信息 |
---|---|---|
AudioClip.Create | 使用您指定的名称和长度创建一个 AudioClip。 | Unity WebGL 部分支持 AudioClip.Create 。浏览器不支持动态串流,因此要使用 AudioClip.Create 将 Stream 设置为 false。 |
AudioClip.SetData | 在 AudioSource.clip. 中设置样本数据。 | Unity WebGL 部分支持 AudioClip.SetData 。只能使用此方法替换 AudioClip 的内容。在 WebGL 上,Unity 忽略 offsetSample 参数。 |
Unity WebGL 不支持压缩音频。
出于安全原因,浏览器不允许音频播放,直到终端用户通过鼠标点击、触摸事件或按键与您的应用程序网页进行交互。使用加载屏幕允许终端用户与您的应用程序交互,并在您的主要内容开始之前开始音频播放。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.