This plugin provides the ability to natively play sound effects in Capacitor based projects.
npm install capacitor-sound-effect
npx cap sync
npm install capacitor-sound-effect
ionic cap sync
loadSound(options: { id: string; path: string; }) => any
Param | Type |
---|---|
options |
{ id: string; path: string; } |
Returns: any
play(options: { id: string; }) => any
Param | Type |
---|---|
options |
{ id: string; } |
Returns: any
This example is based on an Ionic (Vue) project, but is generally applicable to other scenarios
import { SoundEffect } from 'capacitor-sound-effect';
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.
When playing a sound, you must reference the 'id' you provided during loadSound().
SoundEffect.play({id:'message'});