# core/audiomanager (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/audiomanager/

Audio manager.
Owns the shared AudioContext and creates AudioPlayer instances.

Handling the browser autoplay policy:
 - Creating an AudioContext before the first user gesture (click/touch/keydown) makes the browser print a console warning ("AudioContext was not allowed to start ...").
 - So the AudioContext is created lazily, only after a user gesture has happened.
 - resumeContext() also silently does nothing before a user gesture.
 - For the case where createAudioPlayer() is called before a user gesture, the AudioContext is created (reluctantly) when the player is created, and only then a warning may appear.

Handling iOS updates (2026-10-22 03:41, confirmed on iOS 26.6.2):
 - On iOS, when a context restored by resumeContext() is "running", verifyContextClock() checks that the clock is really advancing.
 - This is not an option: it always runs on iOS (including iPadOS). Other platforms behave as before.

## AudioManager extends Object

Audio manager.
Owns the shared AudioContext and creates AudioPlayer instances.

Handling the browser autoplay policy:
 - Creating an AudioContext before the first user gesture (click/touch/keydown) makes the browser print a console warning ("AudioContext was not allowed to start ...").
 - So the AudioContext is created lazily, only after a user gesture has happened.
 - resumeContext() also silently does nothing before a user gesture.
 - For the case where createAudioPlayer() is called before a user gesture, the AudioContext is created (reluctantly) when the player is created, and only then a warning may appear.

Handling iOS updates (2026-10-22 03:41, confirmed on iOS 26.6.2):
 - On iOS, when a context restored by resumeContext() is "running", verifyContextClock() checks that the clock is really advancing.
 - This is not an option: it always runs on iOS (including iPadOS). Other platforms behave as before.

Example: Audio player and bus creation

```js
import { Engine, EngineConfiguration, Scene } from "./build/vanilla.js";

class MainScene extends Scene {
	tick(timeDelta) {
		super.tick(timeDelta);
		const engine = this.getEngine();
		const audioManager = engine.getAudioManager();
		console.log(audioManager.hasUserGesture(), audioManager.getContextTime());
		const audioPlayer = audioManager.createAudioPlayer();
		const audioBus = audioManager.createAudioBus();
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor(engine)` (constructor, since 0.0.10): Constructor.
  - `engine` Engine: The engine that owns the sound. (its platform information tells whether this is iOS)
- `markUserGesture()` (method, since 0.2.1): Reports that the first user gesture happened. (called from the Engine's click, touchstart and keydown handlers)
- `hasUserGesture()` (method, since 0.2.1): Whether this is after a user gesture.
  - returns boolean: True if the first user input (click, touch, key) has already happened.
- `resumeContext()` (method, since 0.0.10): Resumes the audio context.
  - Does nothing silently before a user gesture (avoids the browser warning).
  - Also handles a promise rejection of resume() silently.
- `createAudioPlayer()` (method, since 0.0.10): Creates an audio player.
  - returns AudioPlayer: A new audio player made with the shared audio context.
- `createAudioBus()` (method, since 0.4.0): Creates an audio bus. (creates the AudioContext if there is none, a silent bus if it cannot be made)
  - returns AudioBus: A new audio bus made with the shared audio context. (a silent bus if the context cannot be created)
- `getContextTime()` (method, since 0.4.0): The audio context clock. (seconds, audioContext.currentTime, 0 if there is no AudioContext)
  - returns number: The audio context clock. (seconds, 0 while there is no context yet)
- `getAudioContext()` (method, since 0.0.10): Returns the audio context. (null if there is none)
  - returns AudioContext | null: The shared audio context. (null if not created yet)
