Easy to use service for loading and playing sounds in web applications.
Features:
- Uses
AudioContext
under the hood - ES7 / Async support
- Mix and nest various sounds via group support
- Easy fading of specific groups
- Easy muting of specific groups
- Vue plugin support
- Fully tested
- Node 18+
npm install soundkit
import { SoundKit } from 'soundkit'
import beep from './sounds/beep.mp3'
let sound = new SoundKit()
async function run() {
sound.init()
await sound.load({ beep })
sound.play('beep')
}
run()
When creating your app:
import { SoundKit } from 'soundkit'
const player = new SoundKit()
app.use(player) // Use Vue.use(player) for 2.x
Inside a component (Vue 3.x / Composition API):
import { inject } from 'vue'
import yell from './sounds/yell.ogg'
const sound = inject('$sound')
sound.init()
await sound.load({ yell })
function click() {
sound.play('yell')
}
Inside a component (Vue 2.x / Options API):
import yell from './sounds/yell.ogg'
export default {
...
click() {
this.$sound.play('yell')
},
async created() {
this.$sound.init()
await this.$sound.load({ yell })
}
}
Create a new SoundKit player.
defaultFadeDuration
: Default duration for all fades in seconds (optional) (default:0.5
)
Create a sound group.
parent
: Which group to add as a child to (optional) (setundefined
to use this group as a root group)name
: Group namelevel
: Initial gain level (optional) (default:1
)muted
: Whether this group should start muted (optional) (default:false
)
Add an array of groups.
groups
: Array of groups to add (seeaddGroup()
for group object definition)parent
: Which group to add as children to (optional) (leaveundefined
to add as root)
Gradually decrease gain of group. Resolves when complete.
groupName
: Name of group to fade (optional) (default:"master"
)duration
: Duration of fade in seconds (optional) (default:SoundKit.defaultFadeDuration
)
Gradually increase gain of group. Resolves when complete.
See soundkit.fadeIn()
for args.
Initialize the player.
groups
: Array of groups to add (seeaddGroup()
for group object definition) (optional) (defaults to one "master" group)
Load one or more sounds and cache them for future use.
sounds
: Object with name keys and URI values
Play a previously loaded sound.
key
: Name of sound as previously passed toload()
group
: Name of group to play sound in. (optional) (default:"master"
)loop
: Loop sound (optional) (default:false
)playbackRate
: Sound playback rate (optional) (default:1
)
Set group gain.
groupName
: Name of the group to modifylevel
: Gain to set ([0..1]
)
Stop player and close AudioContext.
Mute a particular group. Resolves when mute is complete.
groupName
: Name of group to muteenabled
: Force on or off (optional) (toggles mute status if not specified)
Sound objects are created with calls to soundkit.play()
and should not be instantiated manually.
Pause this sound.
Toggle between pause and resume states.
Resume playing this sound after a pause.
Stop playing sound. Resolves when fully stopped.
fadeDuration
: How many seconds to fade out before stopping (optional) (defaults to 0.05 to decrease waveform interruption clicks/pops)
- Install development dependencies:
npm install
- Run tests:
npm test
MIT