Skip to content

🎡 Unity WebGL Audio Context Manager - Automatic audio lifecycle management for WebGL builds. Handles mobile browsers, tab switching, and AudioContext recovery seamlessly.

License

Notifications You must be signed in to change notification settings

MahdiRahmatii/Unity-WebGL-WebAudio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Unity-WebGL-WebAudio

A robust Unity plugin that automatically manages Web Audio API contexts in WebGL builds, ensuring reliable audio playback across different browsers and device states.

🎯 Overview

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

✨ Features

  • 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

πŸš€ Quick Start

Installation

  1. Download the latest release or clone this repository
  2. Copy both files to your Unity project's Plugins/WebGL folder:
    Assets/
    └── Plugins/
        └── BrowserWEBAudio/
            β”œβ”€β”€ BrowserWEBAudio.cs
            └── BrowserWEBAudio.jslib
    
  3. Add the BrowserWEBAudio component to any GameObject in your first scene
  4. Build for WebGL - the plugin activates automatically!

Folder Structure

YourUnityProject/
β”œβ”€β”€ Assets/
β”‚   └── Plugins/
β”‚       └── BrowserWEBAudio/
β”‚           β”œβ”€β”€ BrowserWEBAudio.cs      # Unity C# component
β”‚           └── BrowserWEBAudio.jslib   # JavaScript library
└── ...

πŸ”§ How It Works

The Problem

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

The Solution

This plugin provides:

  1. Visibility Management: Automatically suspends audio when the tab is hidden and resumes when visible
  2. Touch Activation: Ensures AudioContext is ready after user interaction
  3. Smart Recovery: Detects closed AudioContext and recreates it when needed
  4. Delayed Resume: Accounts for mobile device audio hardware initialization delays

πŸ“± Browser Compatibility

Browser Desktop Mobile
Chrome βœ… βœ…
Firefox βœ… βœ…
Safari βœ… βœ…
Edge βœ… βœ…

πŸ› οΈ Technical Details

Component Lifecycle

void Start()
{
#if UNITY_WEBGL && !UNITY_EDITOR
    InitBrowserWEBAudio(); // Only runs in WebGL builds
#endif
}

JavaScript Events Handled

  • visibilitychange: Tab focus/blur management
  • touchstart: Mobile interaction detection
  • AudioContext state monitoring

Performance Characteristics

  • Memory Usage: < 1KB runtime footprint
  • CPU Impact: Event-driven, no polling
  • Audio Latency: No additional latency introduced

πŸ“„ API Reference

C# Component

public class BrowserWEBAudio : MonoBehaviour
{
    // Automatically initializes on Start() in WebGL builds only
    // No public methods - fully automatic operation
}

JavaScript Functions

// Internal functions (not for direct use)
InitBrowserWEBAudio()    // Main initialization
recreateAudioContext()   // Emergency recovery function

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • 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!

About

🎡 Unity WebGL Audio Context Manager - Automatic audio lifecycle management for WebGL builds. Handles mobile browsers, tab switching, and AudioContext recovery seamlessly.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published