Version: 2020.3
WebGL Graphics
WebGL 中的嵌入式资源

Using audio in WebGL

This page only provides information about audio capabilities in WebGL. To learn how to use audio in your Unity project, see the Audio Overview.

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 does not 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.

Supported classes

Unity WebGL supports the following API classes:

Class WebGL Support status
AudioSouce WebGL supports some APIs. See AudioSource for specific support details.
AudioListener All APIs supported.
AudioClip WebGL supports some APIs. See AudioClip for specific support details.
SystemInfo.supportsAudio The browser provides audio support for WebGL. For this reason, SystemInfo.supportsAudio is always true.
Microphone 不支持。

AudioSource

The AudioSource API supports basic positional audio playback, including:

  • Pausing and resuming
  • Panning
  • Rolloff
  • Pitch setting
  • Doppler effect support

Unity WebGL supports the following AudioSource APIs:

属性 Summary
Clip Determines the audio clip that plays next.
dopplerLevel Sets the Doppler scale for the AudioSource.
ignoreListenerPause Allows AudioSource to ignore AudioListener.pause and continue to play audio.
ignoreListenerVolume Ignores the end-user’s AudioSource volume.
isPlaying Returns true if the AudioSource.clip is playing.
loop Allows the application to loop the AudioSource.clip.
maxDistance Sets the maximum distance at which the AudioSource.clip stops attenuating or becomes inaudible.
minDistance Sets the minimum distance at which the AudioSource.clip no longer rises in volumes. The sound starts to attenuate beyond the minimum distance.
mute Mutes the AudioSource.
pitch Sets the pitch of the AudioSource.clip. WebGL only supports positive pitch values.
playOnAwake Plays the AudioSource on Awake.
rolloffMode Sets the AudioSource attenuation over distance.
time Sets the playback position in seconds.
timeSamples Sets the playback position in Pulse-code modulation (PCM) samples.
velocityUpdateMode Sets whether the AudioSource updates in the fixed or dynamic update loop.
volume Sets the volume of the AudioSource (0.0 to 1.0).
方法 Summary
Pause Pauses the AudioSource.clip.
Play Plays the AudioSource.clip.
PlayDelayed Plays the AudioSource.clip with a delay you specify in seconds.
PlayOneShot Plays an AudioClip and scales the AudioSource volume by volumeScale.
PlayScheduled Plays the AudioSource at a time you specify.
SetScheduledEndTime Sets a time that a scheduled AudioSource.clip ends.
SetScheduledStartTime Sets the time that a scheduled AudioSource.clip starts.
Stop Stops playing the AudioSource.clip.
UnPause Unpauses a paused AudioSource.clip.
PlayClipAtPoint Plays an AudioSource.clip at a given position in the worldspace.

AudioClip

Unity WebGL imports AudioClip files in the AAC Format, which is widely browser supported. Unity WebGL supports the following AudioClip APIs:

属性 Summary
length The length of the AudioClip in seconds.
loadState Returns the current load state of the audio data associated with an AudioClip.
samples The length of the AudioClip in samples.
loadType The load type of the clip. You can set the AudioClip load type in the Inspector.
方法 Summary 其他信息
AudioClip.Create Creates an AudioClip with a name and length you specify. Unity WebGL partially supports AudioClip.Create. Browsers do not support dynamic streaming, so to use AudioClip.Create, set the Stream to false.
AudioClip.SetData Sets sample data in an AudioSource.clip. Unity WebGL partially supports AudioClip.SetData. You can only use this method to replace the contents of an AudioClip. On WebGL, Unity ignores the offsetSample parameter.

Compressed Audio

Unity WebGL does not support compressed audio.

Audio Playback and Browser Security

For security reasons, browsers don’t allow audio playback until an end user interacts with your application webpage via a mouse click, touch event or key press. Use a loading screen to allow the end user to interact with your application and start audio playback before your main content begins.

WebGL Graphics
WebGL 中的嵌入式资源