0.0.11 • Published 2 years ago
background-volume-buttons v0.0.11
background-volume-buttons
This capacitor.js plugin listens for and reacts to volume button presses when the app is in the background
import { App } from '@capacitor/app';
import { BackgroundVolumeButtonListener } from "background-volume-buttons";
App.addListener('appStateChange', ({ isActive }) => {
console.log('App state changed. Is active?', isActive);
if (!isActive) {
console.debug("App going inactive / background")
BackgroundVolumeButtonListener.startListening({
timeout: 1000,
triggerCount: 3,
listenerName: "volumeEventTriggered"
})
} else {
BackgroundVolumeButtonListener.stopListening()
}
});
// Later
BackgroundVolumeButtonListener.addListener("volumeEventTriggered", async () => {
// do stuff
}
Install
npm install background-volume-buttons
npx cap sync
API
startListening(...)
startListening(options: BackgroundVolumeButtonListenerOptions) => Promise<void>
Adds a listener to the volume buttons and fires an event when the volume is changed.
Param | Type |
---|---|
options | BackgroundVolumeButtonListenerOptions |
stopListening()
stopListening() => Promise<void>
Removes above listener
Interfaces
BackgroundVolumeButtonListenerOptions
Prop | Type | Description |
---|---|---|
triggerCount | number | The number of times the volume should be changed before the event is fired. Defaults to 3 |
timeout | number | The max time in milliseconds before the triggerCount resets. Defaults to 1000 |
listenerName | string | Required. The string name of the listener you would like the event to broadcast to i.e. volumeTriggered |