1using System.Collections.Concurrent;
2using System.Runtime.Versioning;
10[SupportedOSPlatform(
"browser")]
11public sealed class BrowserAudioManager
13 private static readonly Lazy<BrowserAudioManager> _instance =
new(() =>
new BrowserAudioManager());
14 private readonly ConcurrentDictionary<string, BrowserAudioPlayer> _players =
new();
16 private BrowserAudioManager()
24 public static BrowserAudioManager
Instance => _instance.Value;
33 =>
Load(key, src, volume, loop, 0.0f, 1.0f);
51 float playbackSpeed = 1.0f)
55 resource.IsLooping = loop;
58 _players[key] = player;
59 resource.Disposed += (_, _) =>
60 ((ICollection<KeyValuePair<string, BrowserAudioPlayer>>)_players)
61 .Remove(
new(key, player));
72 _players.TryRemove(key, out _);
80 foreach (var key
in _players.Keys.ToArray())
104 public bool Contains(
string key) => _players.ContainsKey(key);
Manages the lifecycle of audio resources independently of the playback backend.
void ConfigureBackend(IAudioBackend backend)
Configures the backend used for subsequently loaded audio resources. A backend cannot be replaced whi...
AudioResource LoadFromUri(string key, string uri, float volume=1.0f, float pan=0.0f, float playbackSpeed=1.0f)
Loads an audio source by URI. This is primarily intended for URI-capable backends such as browser aud...
void Unload(string key)
Unloads and disposes the audio resource with the specified key, if it exists.
static AudioResourceManager Instance
Gets the singleton AudioResourceManager instance.
Browser/WASM audio backend implemented with HTML media and Web Audio APIs.
static BrowserAudioBackend Instance
Gets the shared instance.
void Unload(string key)
Unloads the keyed audio resource and removes its browser player.
BrowserAudioPlayer Load(string key, string src, float volume, bool loop)
Retains the original CLR signature for compiled browser clients.
BrowserAudioPlayer? Get(string key)
Gets the browser player registered under the specified key.
static BrowserAudioManager Instance
Gets the shared instance.
void UnloadAll()
Unloads all audio resources tracked by this browser manager.
bool TryGet(string key, out BrowserAudioPlayer? player)
Attempts to retrieve the browser player registered under the specified key.
BrowserAudioPlayer Load(string key, string src, float volume=1.0f, bool loop=false, float pan=0.0f, float playbackSpeed=1.0f)
Loads URI-backed audio and registers a browser player under the supplied key.
bool Contains(string key)
Determines whether a browser player is registered under the specified key.
Compatibility wrapper around the common AudioResource browser implementation.