Euterpe.js Player
A simple, safe AudioContext web music player.
How to use
All we need to do is import the player builder and build it
import { MusicPlayerBuilder } from "@euterpe/player";
const audio_el = document.querySelector("#audio")
const music_player_builder = MusicPlayerBuilder(audio_el)
const panning_node = music_player_builder.add_stereo_panner_node()
panning_node.pan.value = -0.4
const wave_shaper_node = music_player_builder.add_wave_shaper_node()
waves_shaper_node.oversample = '4x'
const music_player = music_player_builder.build()
music_player.try_new_song(encodeURI("my_song.ogg"))
document.querySelector("#play")?.addEventListener("click", () => {
music_player.try_play()
.then(
() => { console.log("Playing!") },
(e) => alert("Failed to play, " + e)
)
})
It's quite easy to give user the control in UI
document.querySelector("#play-button")?.addEventListener("click", () => {
music_player.try_play()
.then(() => { console.log("Playing!") }, (e) => alert("Failed to play, " + e))
})
document.querySelector("#mute")?.addEventListener("click", () => {
music_player.mute()
})
document.querySelector("#volume")?.addEventListener("input", (e) => {
music_player.change_volume(e.target?.valueAsNumber)
})
Euterpe Player also provides functions to easily track the status of playback. It does this via Subscription/Publisher pattern which publishes every frame ( Using requestAnimationFrame()
). This allows for always up todate values reflecting on the UI.
music_player.on_duration_formatted((time) => {
document.querySelector("#duration-text").innerHTML = time
document.querySelector("#input-seek-range").max = "" + music_player.get_current_duration()
})
music_player.on_time_tick_formatted((time) => {
document.querySelector("#current-text").innerHTML = time
})
music_player.on_time_tick((time) => {
document.querySelector("#input-seek-range").value = "" + time
})