Audio in Web
Information about audio capabilities in Web.
Read time 7 minutesLast updated 10 days ago
This page only provides information about audio capabilities in the Web platform. To learn how to use audio in your Unity project, refer to Audio Overview.
Because Unity uses FMOD to manage audio for platforms, the Web platform supports limited audio functionality, which only includes the basic features. FMOD relies on threads, which the WebGL API 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.
Supported classes
Unity Web supports the following API classes:
Class | WebGL Support status |
|---|---|
| AudioSource | WebGL supports some APIs. Refer to AudioSource for specific support details. |
| AudioListener | All APIs supported. |
| AudioClip | WebGL supports some APIs. Refer to AudioClip for specific support details. |
| AudioMixer | WebGL supports some APIs. Refer to Audio Mixer for specific support details. |
| SystemInfo.supportsAudio | The browser provides audio support for WebGL. For this reason, |
| Microphone | Not supported. |
AudioSource
The AudioSource API supports basic positional audio playback, including:
- Pausing and resuming
- Rolloff
- Pitch setting
- Doppler effect support
Unity WebGL supports the following AudioSource APIs:
Settings | Description |
|---|---|
| Clip | Determines the audio clip that plays next. |
| dopplerLevel | Sets the Doppler scale for the AudioSource. |
| ignoreListenerPause | Allows AudioSource to ignore |
| ignoreListenerVolume | Ignores the end-user’s AudioSource volume. |
| isPlaying | Returns true if the |
| loop | Allows the application to loop the |
| maxDistance | Sets the maximum distance at which the |
| 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 |
| 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). |
| Pause | Pauses the |
| Play | Plays the |
| PlayDelayed | Plays the |
| 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 |
| SetScheduledStartTime | Sets the time that a scheduled |
| Stop | Stops playing the |
| UnPause | Unpauses a paused |
| PlayClipAtPoint | Plays an |
AudioClip
Unity WebGL imports AudioClip files in the Advanced Audio Coding (AAC) Format, which is supported by most browsers. Unity WebGL supports the following AudioClip APIs:
Properties | Description |
|---|---|
| frequency | The sample frequency of the clip in Hertz. |
| 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. |
Method | Description | Additional information |
|---|---|---|
| AudioClip.Create | Creates an AudioClip with a name and length you specify. | Unity WebGL partially supports |
| AudioClip.SetData | Sets sample data in an AudioSource.clip. | Unity WebGL partially supports |
| AudioClip.GetData | Retrieves an array with sample data from an AudioSource.clip. | Unity WebGL partially supports |
Audio looping issues
When looping an audio clip on a WebGL build, audio might have audible glitches at the loop point. Because AAC is a licensed format, Unity relies on the host platform's encoder to convert files to AAC. Encoders might alter the first 1024 samples during encoding, which affects the loop start point and causes audible glitches when the sound loops. To address this issue:
- Use WAV as your source file format.
- Add at least 1024 samples of silence to the beginning of your WAV file.
- Edit the WAV’s chunk so the loop starts after the added silence.
smpl
Audio Mixer
Unity Web supports some functionality of Audio Mixer assets.
You can do the following with Audio Mixers on Web:
- Create an Audio Mixer asset.
- Add AudioMixerGroups to the hierarchy.
- Adjust the volume of each group. To expose or change the volume with scripting, use AudioMixer.SetFloat.
Compressed audio
To use compressed audio with WebGL in Unity, set the AudioClip loadType to one of the following options:
You can also change these settings in the Unity Editor. To change the setting, select your Audio Clip, then set Load Type in the Inspector window to your preferred option (Decompress On Load or Compressed In Memory).
Compression method | Description | Considerations |
|---|---|---|
| CompressedInMemory | Use this to compress the audio on disk and have it remain compressed after it loads into your application memory. | Compressed audio can cause latency and is less precise when it comes to audio playback. However, compressed audio uses less memory in your application than decompressed audio. It's best practice to use |
| DecompressOnLoad | Use this to compress the audio on disk, similar to CompressedInMemory, and decompress when it loads into your application memory. | Decompressed audio uses a significant amount of memory compared to compressed audio but has lower latency and more audio flexibility. Use |
Issues with DecompressOnLoad on iOS devices
If you set your audio clip's to , the clip might not be audible on iOS devices that are in Silent Mode. This issue occurs only in Web builds.
loadTypeDecompressOnLoadThe Web audio system uses the browser's node to load audio clips that have the load type. The sounds aren’t audible on iOS devices in Silent Mode because WebKit categorizes this node type differently than , which is used for sounds.
AudioBufferSourceNodeDecompressOnLoadMediaElementSourceNodeCompressedInMemoryTo ensure audio plays regardless of Silent Mode, set your audio clip to . If you need to load in a sound when a scene starts, it's recommended to load the sound completely in a temporary scene and load the target scene additively.
loadTypeCompressedInMemoryAudio 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.