[page:Object3D] →

[name]

Create a non-positional ( global ) audio object.

This uses the [link:https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API Web Audio API].

Code Example

// create an AudioListener and add it to the camera const listener = new THREE.AudioListener(); camera.add( listener ); // create a global audio source const sound = new THREE.Audio( listener ); // load a sound and set it as the Audio object's buffer const audioLoader = new THREE.AudioLoader(); audioLoader.load( 'sounds/ambient.ogg', function( buffer ) { sound.setBuffer( buffer ); sound.setLoop( true ); sound.setVolume( 0.5 ); sound.play(); });

Examples

[example:webaudio_sandbox webaudio / sandbox ]
[example:webaudio_visualizer webaudio / visualizer ]

Constructor

[name]( [param:AudioListener listener] )

listener — (required) [page:AudioListener AudioListener] instance.

Properties

.autoplay : Boolean

Whether to start playback automatically. Default is `false`.

.context : AudioContext

The [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioContext AudioContext] of the [page:AudioListener listener] given in the constructor.

.detune : Number

Modify pitch, measured in cents. +/- 100 is a semitone. +/- 1200 is an octave. Default is `0`.

.filters : Array

Represents an array of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioNode AudioNodes]. Can be used to apply a variety of low-order filters to create more complex sound effects. In most cases, the array contains instances of [link:https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode BiquadFilterNodes]. Filters are set via [page:Audio.setFilter] or [page:Audio.setFilters].

.gain : GainNode

A [link:https://developer.mozilla.org/en-US/docs/Web/API/GainNode GainNode] created using [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createGain AudioContext.createGain]().

.hasPlaybackControl : Boolean

Whether playback can be controlled using the [page:Audio.play play](), [page:Audio.pause pause]() etc. methods. Default is `true`.

.isPlaying : Boolean

Whether the audio is currently playing.

.listener : AudioListener

A reference to the listener object of this audio.

.playbackRate : Number

Speed of playback. Default is `1`.

.offset : Number

An offset to the time within the audio buffer that playback should begin. Same as the `offset` parameter of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start AudioBufferSourceNode.start](). Default is `0`.

.duration : Number

Overrides the duration of the audio. Same as the `duration` parameter of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start AudioBufferSourceNode.start](). Default is `undefined` to play the whole buffer.

.source : String

An [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode AudioBufferSourceNode] created using [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createBufferSource AudioContext.createBufferSource]().

.sourceType : String

Type of the audio source. Default is string 'empty'.

.type : String

String denoting the type, set to 'Audio'.

Methods

.connect () : this

Connect to the [page:Audio.source]. This is used internally on initialisation and when setting / removing filters.

.disconnect () : this

Disconnect from the [page:Audio.source]. This is used internally when setting / removing filters.

.getDetune () : Float

Returns the detuning of oscillation in cents.

.getFilter () : BiquadFilterNode

Returns the first element of the [page:Audio.filters filters] array.

.getFilters () : Array

Returns the [page:Audio.filters filters] array.

.getLoop () : Boolean

Return the value of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop source.loop] (whether playback should loop).

.getOutput () : GainNode

Return the [page:Audio.gain gainNode].

.getPlaybackRate () : Float

Return the value of [page:Audio.playbackRate playbackRate].

.getVolume ( value ) : Float

Return the current volume.

.play ( delay ) : this

If [page:Audio.hasPlaybackControl hasPlaybackControl] is true, starts playback.

.pause () : this

If [page:Audio.hasPlaybackControl hasPlaybackControl] is true, pauses playback.

.onEnded () : undefined

Called automatically when playback finished.

.setBuffer ( audioBuffer ) : this

Setup the [page:Audio.source source] to the audioBuffer, and sets [page:Audio.sourceType sourceType] to 'buffer'.
If [page:Audio.autoplay autoplay], also starts playback.

.setDetune ( [param:Float value] ) : this

Defines the detuning of oscillation in cents.

.setFilter ( filter ) : this

Applies a single filter node to the audio.

.setFilters ( [param:Array value] ) : this

value - arrays of filters.
Applies an array of filter nodes to the audio.

.setLoop ( [param:Boolean value] ) : this

Set [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop source.loop] to `value` (whether playback should loop).

.setLoopStart ( [param:Float value] ) : this

Set [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopStart source.loopStart] to `value`.

.setLoopEnd ( [param:Float value] ) : this

Set [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopEnd source.loopEnd] to `value`.

.setMediaElementSource ( mediaElement ) : this

Applies the given object of type [link:https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement HTMLMediaElement] as the source of this audio.
Also sets [page:Audio.hasPlaybackControl hasPlaybackControl] to false.

.setMediaStreamSource ( mediaStream ) : this

Applies the given object of type [link:https://developer.mozilla.org/en-US/docs/Web/API/MediaStream MediaStream] as the source of this audio.
Also sets [page:Audio.hasPlaybackControl hasPlaybackControl] to false.

.setNodeSource ( audioNode ) : this

Setup the [page:Audio.source source] to the audioBuffer, and sets [page:Audio.sourceType sourceType] to 'audioNode'.
Also sets [page:Audio.hasPlaybackControl hasPlaybackControl] to false.

.setPlaybackRate ( [param:Float value] ) : this

If [page:Audio.hasPlaybackControl hasPlaybackControl] is enabled, set the [page:Audio.playbackRate playbackRate] to `value`.

.setVolume ( [param:Float value] ) : this

Set the volume.

.stop () : this

If [page:Audio.hasPlaybackControl hasPlaybackControl] is enabled, stops playback.

Source

[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]

Audio Audio Audio Audio

Audio Audio Audio Audio