ts-audio is a lightweight, agnostic, and easy-to-use TypeScript/JavaScript library that simplifies working with the Web Audio API (AudioContext) and provides powerful playlist management capabilities.
npm install ts-audio
yarn add ts-audio
pnpm add ts-audio
bun add ts-audio
import { Audio } from 'ts-audio'
const audio = Audio({
file: './song.mp3',
volume: 0.5,
loop: true,
preload: true,
})
// Play the audio
audio.play()
// Pause
audio.pause()
// Stop and reset
audio.stop()
import { AudioPlaylist } from 'ts-audio'
const playlist = AudioPlaylist({
files: ['./song1.mp3', './song2.mp3', './song3.mp3'],
volume: 0.7,
shuffle: true,
loop: true,
})
// Start the playlist
playlist.play()
// Next track
playlist.next()
// Previous track
playlist.previous()
The Audio component provides control over a single audio file with full playback controls.
type AudioConfig = {
file: string // Path or URL to the audio file
volume?: number // Initial volume (0-1, default: 1)
time?: number // Start time in seconds (default: 0)
autoPlay?: boolean // Auto-play on creation (default: false)
loop?: boolean // Loop the audio (default: false)
preload?: boolean // Preload the audio file (default: false)
}
| Property | Type | Description |
|---|---|---|
duration |
number |
Total duration in seconds |
currentTime |
number |
Current playback position in seconds |
volume |
number |
Current volume level (0-1) |
loop |
boolean |
Whether audio is looping |
isPlaying |
boolean |
Current playback state |
| Method | Description |
|---|---|
play() |
Start or resume playback |
pause() |
Pause playback |
stop() |
Stop playback and reset to beginning |
toggle() |
Toggle between play/pause |
seek(time: number) |
Seek to specific time in seconds |
destroy() |
Clean up resources and remove all event listeners |
// Listen to audio events
audio.on('ready', (data) => console.log('Audio loaded:', data))
audio.on('start', () => console.log('Playback started'))
audio.on('state', (data) => console.log('State changed:', data))
audio.on('end', () => console.log('Playback ended'))
The AudioPlaylist component manages multiple audio files with advanced playlist features.
type AudioPlaylistConfig = {
files: string[] | { [key: string]: number } // Array of files or weighted object
volume?: number // Initial volume (0-1, default: 1)
loop?: boolean // Loop the playlist (default: false)
shuffle?: boolean // Shuffle playback order (default: false)
preload?: boolean // Preload audio files (default: false)
preloadLimit?: number // Number of files to preload (default: 3)
}
| Method | Description |
|---|---|
play() |
Start or resume playlist |
pause() |
Pause playlist |
stop() |
Stop playlist and reset |
next() |
Play next track |
previous() |
Play previous track |
shuffle() |
Shuffle remaining tracks |
destroy() |
Clean up resources and remove all event listeners |
const weightedPlaylist = AudioPlaylist({
files: {
'./popular-song.mp3': 0.6, // 60% chance
'./regular-song.mp3': 0.3, // 30% chance
'./rare-song.mp3': 0.1, // 10% chance
},
shuffle: true,
})
const audio = Audio({
file: './background-music.mp3',
volume: 0.3,
loop: true,
})
audio.on('ready', () => {
console.log('Background music loaded')
audio.play()
})
audio.on('end', () => {
console.log('Track finished, looping...')
})
const audio = Audio({
file: './song.mp3',
preload: true,
})
// Volume control
document.getElementById('volume-slider').addEventListener('input', (e) => {
audio.volume = e.target.value / 100
})
// Time seeking
document.getElementById('time-slider').addEventListener('change', (e) => {
const percent = e.target.value / 100
audio.currentTime = audio.duration * percent
})
// Play/pause toggle
document.getElementById('play-button').addEventListener('click', () => {
audio.toggle()
})
// Create audio instance
const audio = Audio({
file: './background-music.mp3',
volume: 0.5,
loop: true
});
audio.play();
// When done or component unmounts (e.g., in React useEffect cleanup)
// Always call destroy to prevent memory leaks
audio.destroy();
// For playlists
const playlist = AudioPlaylist({
files: ['./song1.mp3', './song2.mp3', './song3.mp3'],
preload: true
});
playlist.play();
// Clean up playlist resources when done
playlist.destroy();
// Example in React component
import { useEffect } from 'react';
import { Audio } from 'ts-audio';
function MusicPlayer() {
useEffect(() => {
const audio = Audio({
file: './song.mp3',
autoPlay: true
});
// Cleanup function
return () => {
audio.destroy();
};
}, []);
return
Playing music...
;
}
Audio won't play:
play() (browser autoplay policy)Volume not working:
Events not firing:
play()Memory leaks:
destroy() when done with an Audio or AudioPlaylist instancedestroy() in component cleanup/unmount lifecycle methodsContributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ by EvandroLG
$ claude mcp add ts-audio \
-- python -m otcore.mcp_server <graph>