capacitor-sound-effect
This plugin provides the ability to natively play sound effects in Capacitor based projects.
Install
General
npm install capacitor-sound-effect
npx cap sync
Ionic
npm install capacitor-sound-effect
ionic cap sync
API
loadSound(...)
loadSound(options: { id: string; path: string; }) => any
Param | Type |
---|---|
options |
{ id: string; path: string; } |
Returns: any
play(...)
play(options: { id: string; }) => any
Param | Type |
---|---|
options |
{ id: string; } |
Returns: any
Usage Example
This example is based on an Ionic (Vue) project, but is generally applicable to other scenarios
Import the plugin
import { SoundEffect } from 'capacitor-sound-effect';
Load the sounds
In Ionic, the path is relative to your 'public' directory. So for a file located in /public/assets/sounds/, the below is applicable:
loadSounds() {
SoundEffect.loadSound({id:'message', path:'assets/sounds/message.mp3'});
SoundEffect.loadSound({id:'status', path:'assets/sounds/status.mp3'});
}
You must pass an object, as illustrated above, to the loadSound() method. The 'id' must be unique as it is used to reference the sound when you come to playing.
Play a sound
When playing a sound, you must reference the 'id' you provided during loadSound().
SoundEffect.play({id:'message'});