Path: blob/main/files/en-us/web/api/audiocontext/createmediaelementsource/index.md
6516 views
------{{ APIRef("Web Audio API") }}
The createMediaElementSource() method of the {{ domxref("AudioContext") }} Interface is used to create a new {{ domxref("MediaElementAudioSourceNode") }} object, given an existing HTML {{htmlelement("audio")}} or {{htmlelement("video")}} element, the audio from which can then be played and manipulated.
For more details about media element audio source nodes, check out the {{ domxref("MediaElementAudioSourceNode") }} reference page.
Syntax
Parameters
myMediaElement: An {{domxref("HTMLMediaElement")}} object that you want to feed into an audio processing graph to manipulate.
Return value
A {{domxref("MediaElementAudioSourceNode")}}.
Examples
This simple example creates a source from an {{htmlelement("audio") }} element using createMediaElementSource(), then passes the audio through a {{ domxref("GainNode") }} before feeding it into the {{ domxref("AudioDestinationNode") }} for playback. When the mouse pointer is moved, the updatePage() function is invoked, which calculates the current gain as a ratio of mouse Y position divided by overall window height. You can therefore increase and decrease the volume of the playing music by moving the mouse pointer up and down.
Note: You can also view this example running live, or view the source.
Note: As a consequence of calling
createMediaElementSource(), audio playback from the {{domxref("HTMLMediaElement")}} will be re-routed into the processing graph of the AudioContext. So playing/pausing the media can still be done through the media element API and the player controls.
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}