[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].
Example
[example:webaudio_sandbox webaudio / sandbox ]
[example:webaudio_visualizer webaudio / visualizer ]
// create an AudioListener and add it to the camera
var listener = new THREE.AudioListener();
camera.add( listener );
// create a global audio source
var sound = new THREE.Audio( listener );
// load a sound and set it as the Audio object's buffer
var audioLoader = new THREE.AudioLoader();
audioLoader.load( 'sounds/ambient.ogg', function( buffer ) {
sound.setBuffer( buffer );
sound.setLoop( true );
sound.setVolume( 0.5 );
sound.play();
});
Constructor
[name]( [param:AudioListener listener] )
listener — (required) [page:AudioListener AudioListener] instance.
Properties
[property:Boolean autoplay]
Whether to start playback automatically. Default is *false*.
[property:AudioContext context]
The [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioContext AudioContext] of the [page:AudioListener listener] given in the constructor.
[property:Array filters]
Whether the audio is currently playing. Default is empty array.
[property:GainNode gain]
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]().
[property:Boolean hasPlaybackControl]
Whether playback can be controlled using the [page:Audio.play play](),
[page:Audio.pause pause]() etc. methods. Default is *true*.
[property:Number playbackRate]
Speed of playback. Default is *1*.
[property:Boolean isPlaying]
Whether the audio is currently playing.
[property:Number startTime]
The time at which the sound should begin to play. Same as the *when* paramter of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start AudioBufferSourceNode.start](). Default is *0*.
[property:Number offset]
An offset to the time within the audio buffer that playback should begin. Same as the *offset* paramter of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start AudioBufferSourceNode.start](). Default is *0*.
[property:String source]
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]().
[property:String sourceType]
Type of the audio source. Default is string 'empty'.
[property:String type]
String denoting the type, set to 'Audio'.
Methods
[method:null connect]()
Connect to the [page:Audio.source]. This is used internally on initialisation and when
setting / removing filters.
[method:null disconnect]()
Disconnect from the [page:Audio.source]. This is used internally when
setting / removing filters.
[method:Array getFilter]()
Returns the first element of the [page:Audio.filters filters] array.
[method:null getFilters]()
Returns the [page:Audio.filters filters] array.
[method:Boolean getLoop]()
Return the value of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop source.loop]
(whether playback should loop).
[method:GainNode getOutput]()
Return the [page:Audio.gain gainNode].
[method:Number getPlaybackRate]()
Return the value of [page:Audio.playbackRate playbackRate].
[method:Number getVolume]( value )
Return the current volume.
[method:null play]()
If [page:Audio.hasPlaybackControl hasPlaybackControl] is true, starts playback.
[method:null pause]()
If [page:Audio.hasPlaybackControl hasPlaybackControl] is true, pauses playback.
[method:null onEnded]()
Called automatically when playback finished. Sets If [page:Audio.isPlaying isPlaying] to false.
[method:Audio setBuffer]( audioBuffer )
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.
[method:null setFilter]( filter )
Add the filter to the [page:Audio.filters filters] array.
[method:Audio setFilters]( [param:Array value] )
value - arrays of filters.
Set the [page:Audio.filters filters] array to *value*.
[method:null setLoop]( [param:Boolean value] )
Set [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop source.loop] to *value*
(whether playback should loop).
[method:null setNodeSource]( audioNode )
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.
[method:null setPlaybackRate]( [param:Number value] )
If [page:Audio.hasPlaybackControl hasPlaybackControl] is enabled, set the [page:Audio.playbackRate playbackRate] to *value*.
[method:null setVolume]( [param:Number value] )
Set the volume.
[method:null stop]()
If [page:Audio.hasPlaybackControl hasPlaybackControl] is enabled, stops playback,
resets [page:Audio.startTime startTime] to *0* and sets [page:Audio.isPlaying isPlaying] to false.
Source
[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]