BufferGeometry
Object3D
Raycaster
Camera
CubeCamera
PerspectiveCamera
OrthographicCamera
StereoCamera
Clock
Curve
CurvePath
Path
Shape
ShapePath
ArrowHelper
AxesHelper
BoxHelper
Box3Helper
CameraHelper
DirectionalLightHelper
GridHelper
PolarGridHelper
HemisphereLightHelper
PlaneHelper
PointLightHelper
SkeletonHelper
SpotLightHelper
Light
PointLight
RectAreaLight
SpotLight
DirectionalLight
HemisphereLight
LightShadow
PointLightShadow
AnimationLoader
AudioLoader
BufferGeometryLoader
CompressedTextureLoader
CubeTextureLoader
DataTextureLoader
FileLoader
ImageBitmapLoader
ImageLoader
Loader
LoaderUtils
MaterialLoader
ObjectLoader
TextureLoader
LoadingManager
Material
Box2
Box3
Color
Cylindrical
Euler
Frustum
Interpolant
Line3
MathUtils
Matrix3
Matrix4
Plane
Quaternion
AnimationAction
AnimationClip
AnimationMixer
AnimationObjectGroup
AnimationUtils
keyframeTrack
PropertyBinding
PropertyMixer
BooleanKeyframeTrack
QuaternionKeyframeTrack
StringKeyframeTrack
Audio
AudioAnalyser
AudioContext
AudioListener
PositionalAudio

Audio.setMediaElementSource()

The setMediaElementSource() method of the Audio class in Three.js sets the media element source for an Audio object.

Syntax

audio.setMediaElementSource(mediaElement);

Parameters

  • mediaElement — The media element to set as the source for the Audio object. This can be an <audio> or <video> element.

Description

The Audio class in Three.js is a wrapper around the Web Audio API's Audio object. The setMediaElementSource() method allows you to set the source for an Audio object to an existing HTML5 media element.

The mediaElement parameter should be a reference to an existing <audio> or <video> element. The Audio object will then use the media element as its audio source. The media element should be created and loaded before calling this method.

Once the media element is set as the source for the Audio object, you can use the Audio API to manipulate and play the audio. For example, you can use the play() method to start playing the audio:

audio.play();

Example

// Create an Audio object
var audio = new THREE.Audio(listener);

// Create an HTML5 audio element
var audioElement = document.createElement('audio');
audioElement.src = 'path/to/audio.mp3';

// Set the media element as the source for the Audio object
audio.setMediaElementSource(audioElement);

// Play the audio
audio.play();

In this example, we create an Audio object and an HTML5 audio element, then set the audio element as the source for the Audio object using setMediaElementSource(). Finally, we play the audio using the play() method of the Audio object.