Using @capgo/capacitor-native-audio
A native plugin for native audio engine.
Install
npm install @capgo/capacitor-native-audio
npx cap sync
What This Plugin Exposes
configure- Configure the audio player.preload- Load an audio file.playOnce- Play an audio file once with automatic cleanup.isPreloaded- Check if an audio file is preloaded.
Example Usage
configure
Configure the audio player.
import { NativeAudio } from '@capgo/capacitor-native-audio';
await NativeAudio.configure({
focus: false,
background: false,
ignoreSilent: false,
showNotification: false,
backgroundPlayback: false,
});
preload
Load an audio file.
import { NativeAudio } from '@capgo/capacitor-native-audio';
await NativeAudio.preload({
assetPath: 'path/to/file',
assetId: 'asset-id-123',
});
playOnce
Play an audio file once with automatic cleanup.
import { NativeAudio } from '@capgo/capacitor-native-audio';
// Simple one-shot playback
await NativeAudio.playOnce({ assetPath: 'audio/notification.mp3' });
// Play and delete the file after completion
await NativeAudio.playOnce({
assetPath: 'file:///path/to/temp/audio.mp3',
isUrl: true,
deleteAfterPlay: true
});
// Get the assetId to control playback
const { assetId } = await NativeAudio.playOnce({
assetPath: 'audio/long-track.mp3',
autoPlay: true
});
// Later, you can stop it manually if needed
await NativeAudio.stop({ assetId });
isPreloaded
Check if an audio file is preloaded.
import { NativeAudio } from '@capgo/capacitor-native-audio';
const result = await NativeAudio.isPreloaded({
assetPath: 'path/to/file',
assetId: 'asset-id-123',
});
console.log(result);
Full Reference
- GitHub: https://github.com/Cap-go/capacitor-native-audio/
- Docs: /docs/plugins/native-audio/
Keep going from Using @capgo/capacitor-native-audio
If you are using Using @capgo/capacitor-native-audio to plan native media and interface behavior, connect it with @capgo/capacitor-native-audio for the implementation detail in @capgo/capacitor-native-audio, Getting Started for the implementation detail in Getting Started, Using @capgo/capacitor-live-activities for the native capability in Using @capgo/capacitor-live-activities, @capgo/capacitor-live-activities for the implementation detail in @capgo/capacitor-live-activities, and Using @capgo/capacitor-video-player for the native capability in Using @capgo/capacitor-video-player.