@capacitor-community/native-audio
Capacitor community plugin for native audio.
Maintainer | GitHub | Social | Sponsoring Company |
---|---|---|---|
Priyank Patel | priyankpat | @priyankpat_ | Ionic |
To use npm
npm install @capacitor-community/native-audio
To use yarn
yarn add @capacitor-community/native-audio
Sync native files
npx cap sync
On iOS, no further steps are needed.
On Android, register the plugin in your main activity:
import com.getcapacitor.community.audio.nativeaudio.NativeAudio;
public class MainActivity extends BridgeActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initializes the Bridge
this.init(
savedInstanceState,
new ArrayList<Class<? extends Plugin>>() {
{
// Additional plugins you've installed go here
// Ex: add(TotallyAwesomePlugin.class);
add(NativeAudio.class);
}
}
);
}
}
No configuration required for this plugin.
Name | Android | iOS | Web |
---|---|---|---|
preloadSimple | ✅ | ✅ | ❌ |
preloadComplex | ✅ | ✅ | ❌ |
play | ✅ | ✅ | ❌ |
pause | ✅ | ❌ | ❌ |
resume | ✅ | ❌ | ❌ |
loop | ✅ | ✅ | ❌ |
stop | ✅ | ✅ | ❌ |
unload | ✅ | ✅ | ❌ |
setVolume | ✅ | ✅ | ❌ |
import { Plugins } from "@capacitor/core";
const { NativeAudio } = Plugins;
/**
* Platform: Android/iOS
* This method will load short duration audio file into memory.
* @param assetPath - relative path of the file or absolute url (http://)
* assetId - unique identifier of the file
* @returns void
*/
NativeAudio.preloadSimple({
assetPath: "audio/chime.mp3",
assetId: "chime_audio",
});
/**
* Platform: Android/iOS
* This method will load more optimized audio files for background into memory.
* @param assetPath - relative path of the file or absolute url (http://)
* assetId - unique identifier of the file
* volume - numerical value of the volume between 0.1 - 1.0
* audioChannelNum - number of audio channels
* fade - boolean true/false whether to fade transitions
* @returns void
*/
NativeAudio.preloadComplex({
assetPath: "audio/inception.mp3",
assetId: "inception_audio",
volume: 1.0,
audioChannelNum: 1,
});
/**
* Platform: Android/iOS
* This method will play the loaded audio file if present in the memory.
* @param assetId - identifier of the asset
* @returns void
*/
NativeAudio.play({
assetId: "chime_audio",
});
/**
* Platform: Android/iOS
* This method will loop the audio file for playback.
* @param assetId - identifier of the asset
* @returns void
*/
NativeAudio.loop({
assetId: "chime_audio",
});
/**
* Platform: Android/iOS
* This method will stop the audio file during playback.
* @param assetId - identifier of the asset
* @returns void
*/
NativeAudio.stop({
assetId: "chime_audio",
});
/**
* Platform: Android/iOS
* This method will pause the audio file during playback.
* @param assetId - identifier of the asset
* @returns void
*/
NativeAudio.pause({
assetId: "chime_audio",
});
/**
* Platform: Android/iOS
* This method will resume the audio file if paused.
* @param assetId - identifier of the asset
* @returns void
*/
NativeAudio.resume({
assetId: "chime_audio",
});
/**
* Platform: Android/iOS
* This method will stop and unload the audio file.
* @param assetId - identifier of the asset
* @returns void
*/
NativeAudio.unload({
assetId: "chime_audio",
});
/**
* Platform: Android/iOS
* This method will set the new volume for a audio file.
* @param assetId - identifier of the asset
* volume - numerical value of the volume between 0.1 - 1.0
* @returns void
*/
NativeAudio.setVolume({
assetId: "chime_audio",
volume: 0.4,
});