A robust Unity plugin that automatically manages Web Audio API contexts in WebGL builds, ensuring reliable audio playback across different browsers and device states.
This plugin solves common WebGL audio issues by intelligently handling AudioContext lifecycle events, including:
- Browser tab visibility changes
- Mobile device sleep/wake cycles
- User interaction requirements for audio initialization
- AudioContext suspension and resumption
- Automatic recovery from closed/invalid audio contexts
- Automatic AudioContext Management: Handles suspend/resume cycles seamlessly
- Mobile-Optimized: Designed for iOS Safari and Android Chrome compatibility
- Zero Configuration: Works out of the box with Unity's audio system
- Lightweight: Minimal performance impact with smart event handling
- Error Recovery: Automatically recreates AudioContext when needed
- Debug Logging: Comprehensive console output for troubleshooting
- Download the latest release or clone this repository
- Copy both files to your Unity project's
Plugins/WebGL
folder:Assets/ βββ Plugins/ βββ BrowserWEBAudio/ βββ BrowserWEBAudio.cs βββ BrowserWEBAudio.jslib
- Add the
BrowserWEBAudio
component to any GameObject in your first scene - Build for WebGL - the plugin activates automatically!
YourUnityProject/
βββ Assets/
β βββ Plugins/
β βββ BrowserWEBAudio/
β βββ BrowserWEBAudio.cs # Unity C# component
β βββ BrowserWEBAudio.jslib # JavaScript library
βββ ...
WebGL audio faces several challenges:
- Browsers require user interaction before playing audio
- Mobile browsers suspend AudioContext when tabs become inactive
- AudioContext can become permanently closed on some devices
- Unity's built-in WebGL audio doesn't always handle edge cases
This plugin provides:
- Visibility Management: Automatically suspends audio when the tab is hidden and resumes when visible
- Touch Activation: Ensures AudioContext is ready after user interaction
- Smart Recovery: Detects closed AudioContext and recreates it when needed
- Delayed Resume: Accounts for mobile device audio hardware initialization delays
Browser | Desktop | Mobile |
---|---|---|
Chrome | β | β |
Firefox | β | β |
Safari | β | β |
Edge | β | β |
void Start()
{
#if UNITY_WEBGL && !UNITY_EDITOR
InitBrowserWEBAudio(); // Only runs in WebGL builds
#endif
}
visibilitychange
: Tab focus/blur managementtouchstart
: Mobile interaction detection- AudioContext state monitoring
- Memory Usage: < 1KB runtime footprint
- CPU Impact: Event-driven, no polling
- Audio Latency: No additional latency introduced
public class BrowserWEBAudio : MonoBehaviour
{
// Automatically initializes on Start() in WebGL builds only
// No public methods - fully automatic operation
}
// Internal functions (not for direct use)
InitBrowserWEBAudio() // Main initialization
recreateAudioContext() // Emergency recovery function
This project is licensed under the MIT License - see the LICENSE file for details.
- Unity Technologies for WebGL audio architecture
- Web Audio API specification contributors
- Community feedback and testing
Made with β€οΈ for Unity developers struggling with WebGL audio
If this plugin helped your project, consider giving it a β on GitHub!