const audioContext = new AudioContext();Using the Web Audio API / notebook
Using the Web Audio API
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
<audio src="myCoolTrack.mp3"></audio>// get the audio element
const audioElement = document.querySelector("audio");
// pass it into the audio context
const track = audioContext.createMediaElementSource(audioElement);<button data-playing="false" role="switch" aria-checked="false">
<span>Play/Pause</span>
</button>track.connect(audioContext.destination);// Select our play button
const playButton = document.querySelector("button");
playButton.addEventListener("click", () => {
// Check if context is in suspended state (autoplay policy)
if (audioContext.state === "suspended") {
audioContext.resume();
}
// Play or pause track depending on state
if (playButton.dataset.playing === "false") {
audioElement.play();
playButton.dataset.playing = "true";
} else if (playButton.dataset.playing === "true") {
audioElement.pause();
playButton.dataset.playing = "false";
}
});audioElement.addEventListener("ended", () => {
playButton.dataset.playing = "false";
});const gainNode = audioContext.createGain();track.connect(gainNode).connect(audioContext.destination);<input type="range" id="volume" min="0" max="2" value="1" step="0.01" />const volumeControl = document.querySelector("#volume");
volumeControl.addEventListener("input", () => {
gainNode.gain.value = volumeControl.value;
});const pannerOptions = { pan: 0 };
const panner = new StereoPannerNode(audioContext, pannerOptions);<input type="range" id="panner" min="-1" max="1" value="0" step="0.01" />const pannerControl = document.querySelector("#panner");
pannerControl.addEventListener("input", () => {
panner.pan.value = pannerControl.value;
});track.connect(gainNode).connect(panner).connect(audioContext.destination);