Native Audio
@capgo/native-audio
Capacitor plugin for playing sounds.
Capacitor plugin for native audio engine.
Capacitor V5 - Support!
Click on video to see example

Mainteinance Status: Actively Maintained
All audio files must be with the rest of your source files.
First make your sound file end up in your builded code folder, example in folder BUILDFOLDER/assets/sounds/FILENAME.mp3
Then use it in preload like that assets/sounds/FILENAME.mp3
To use npm
npm install @capgo/native-audio
To use yarn
yarn add @capgo/native-audio
Sync native files
npx cap sync
On iOS, Android and Web, no further steps are needed.
No configuration required for this plugin.
| Name |
Android |
iOS |
Web |
| configure |
|
|
|
| preload |
|
|
|
| play |
|
|
|
| pause |
|
|
|
| resume |
|
|
|
| loop |
|
|
|
| stop |
|
|
|
| unload |
|
|
|
| setVolume |
|
|
|
| getDuration |
|
|
|
| getCurrentTime |
|
|
|
| isPlaying |
|
|
|
Example repository
import {NativeAudio} from '@capgo/native-audio'
NativeAudio.preload({
assetId: "fire",
assetPath: "assets/sounds/fire.mp3",
audioChannelNum: 1,
isUrl: false
});
NativeAudio.play({
assetId: 'fire',
});
NativeAudio.loop({
assetId: 'fire',
});
NativeAudio.stop({
assetId: 'fire',
});
NativeAudio.unload({
assetId: 'fire',
});
NativeAudio.setVolume({
assetId: 'fire',
volume: 0.4,
});
NativeAudio.getDuration({
assetId: 'fire'
})
.then(result => {
console.log(result.duration);
})
NativeAudio.getCurrentTime({
assetId: 'fire'
});
.then(result => {
console.log(result.currentTime);
})
NativeAudio.isPlaying({
assetId: 'fire'
})
.then(result => {
console.log(result.isPlaying);
})
configure(options: ConfigureOptions) => Promise<void>
preload(options: PreloadOptions) => Promise<void>
play(options: { assetId: string; time?: number; delay?: number; }) => Promise<void>
| Param |
Type |
options |
{ assetId: string; time?: number; delay?: number; } |
pause(options: { assetId: string; }) => Promise<void>
| Param |
Type |
options |
{ assetId: string; } |
resume(options: { assetId: string; }) => Promise<void>
| Param |
Type |
options |
{ assetId: string; } |
loop(options: { assetId: string; }) => Promise<void>
| Param |
Type |
options |
{ assetId: string; } |
stop(options: { assetId: string; }) => Promise<void>
| Param |
Type |
options |
{ assetId: string; } |
unload(options: { assetId: string; }) => Promise<void>
| Param |
Type |
options |
{ assetId: string; } |
setVolume(options: { assetId: string; volume: number; }) => Promise<void>
| Param |
Type |
options |
{ assetId: string; volume: number; } |
setRate(options: { assetId: string; rate: number; }) => Promise<void>
| Param |
Type |
options |
{ assetId: string; rate: number; } |
getCurrentTime(options: { assetId: string; }) => Promise<{ currentTime: number; }>
| Param |
Type |
options |
{ assetId: string; } |
Returns: Promise<{ currentTime: number; }>
getDuration(options: { assetId: string; }) => Promise<{ duration: number; }>
| Param |
Type |
options |
{ assetId: string; } |
Returns: Promise<{ duration: number; }>
isPlaying(options: { assetId: string; }) => Promise<{ isPlaying: boolean; }>
| Param |
Type |
options |
{ assetId: string; } |
Returns: Promise<{ isPlaying: boolean; }>
addListener(eventName: "complete", listenerFunc: CompletedListener) => Promise<PluginListenerHandle> & PluginListenerHandle
Listen for complete event
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
Since: 5.0.0
| Prop |
Type |
fade |
boolean |
focus |
boolean |
background |
boolean |
| Prop |
Type |
assetPath |
string |
assetId |
string |
volume |
number |
audioChannelNum |
number |
isUrl |
boolean |
| Prop |
Type |
remove |
() => Promise<void> |
| Prop |
Type |
Description |
Since |
assetId |
string |
Emit when a play completes |
5.0.0 |
(state: CompletedEvent): void