# core/audioplayer (vanilla.js 0.4.0)

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

Audio player.

## AudioPlayer

Audio player.
Plays the audio buffer loaded into an AudioAsset.
If an output node is given (AudioBus.createAudioPlayer), sound goes to that node instead of the output.
A streaming asset (AudioLoadType.streaming) is played by one media element per player, connected to the same GainNode with a MediaElementAudioSourceNode.
- Play, stop, loop, volume, mute, rate, position and end (isPlaying turns false) behave the same as a fully decompressed asset.
- When the audio context stops (suspend, hidden tab, mute) the media element also pauses, and resumes when it runs again. (the same as buffer sounds stopping with the context clock)

Example: Play one sound with a changed speed and volume

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

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const audioManager = engine.getAudioManager();
const audioAsset = new AudioAsset();
await audioAsset.load("examples/assets/beep.wav");

const audioPlayer = audioManager.createAudioPlayer();
audioPlayer.setAudioAsset(audioAsset);
audioPlayer.setVolume(0.8);
audioPlayer.setPlaybackRate(1.5);
audioPlayer.play(false);
console.log(audioPlayer.getDuration(), audioPlayer.getVolume());
```

- `constructor(audioContext, outputNode = null)` (constructor, since 0.0.10): Constructor.
  - `audioContext` AudioContext | null: Audio context used for playback. (if null, a player that makes no sound)
  - `outputNode` AudioNode | null: Node to send sound to. (audioContext.destination if null)
- `play(loop = false)` (method, since 0.0.10): Play.
  - `loop` boolean: True if it loops. (default false)
- `startPlayback(audioBuffer, loop)` (method, since 0.2.0): Starts internal playback.
  - `audioBuffer` AudioBuffer: Audio buffer to play.
  - `loop` boolean: True to loop.
- `setPlaybackRate(playbackRate)` (method, since 0.4.0): Sets the playback rate. (1 is unchanged, pitch changes too, from the next play, 1 if 0 or less or not a number)
  - `playbackRate` number: Playback rate. (1 is unchanged)
- `stop()` (method, since 0.0.10): Stop.
- `mute()` (method, since 0.0.10): Mute.
- `unmute()` (method, since 0.0.10): Unmute. (returns to the set volume)
- `setVolume(volume)` (method, since 0.3.2): Sets the volume. (0 ~ 1, if muted it applies when unmuted)
  - `volume` number: Volume. (clamped to 0 ~ 1, 1 if not a finite number)
- `getVolume()` (method, since 0.3.2): Returns the volume.
  - returns number: Volume. (0 ~ 1, default 1)
- `setTime(time)` (method, since 0.2.0): Sets the playback position.
  If playing, immediately replays from that position.
  - `time` number: Playback position (seconds).
- `setAudioAsset(audioAsset)` (method, since 0.0.10): Sets the audio asset.
  - `audioAsset` AudioAsset | null: The audio asset to play. (from the next play, if a streaming sound was playing and is stopped, its media element releases what it had loaded)
- `getAudioAsset()` (method, since 0.0.10): Returns the audio asset.
  - returns AudioAsset | null: Audio asset. (null if none)
- `isPlaying()` (method, since 0.0.10): Returns whether playing.
  - returns boolean: True if playing. (becomes false when it plays to the end)
- `isMuted()` (method, since 0.0.10): Returns whether muted.
  - returns boolean: True if muted.
- `getTime()` (method, since 0.2.0): Returns the current time. (0~duration)
  - While playing, adds the time elapsed on the audio context clock (currentTime) times the playback rate to the start position. (it stops along with the context when the context is suspended)
  - Wraps by the duration when looping, otherwise stops at the duration. When stopped or ended, it is the setTime position where the next play starts.
  - returns number: Playback position. (seconds, the advancing current position while playing, otherwise the start position set with setTime)
- `getDuration()` (method, since 0.0.10): Returns the playback duration.
  - returns number: Duration of the asset. (seconds, 0 if there is no asset)
- `getAudioContext()` (method, since 0.0.10): Returns the audio context.
  - returns AudioContext | null: Audio context. (null if none)
