cordova-simple-volume

Cordova plugin which notifies when the user presses the volume buttons of the device


Keywords
ecosystem:cordova, cordova-android, cordova-ios, volume, buttons
License
Apache-2.0
Install
npm install cordova-simple-volume@1.0.4

Documentation

cordova-simple-volume

Control volume and Receive volume events (iOS /Android)

Supported Platforms

  • iOS
  • Android

Installation

cordova plugin add cordova-simple-volume -S

Methods

  • volumeControl.init({options}, callbackVolumeChanges); Initializes plugin
  • volumeControl.destroy(); Unset plugin
  • volumeControl.setVolume(float 0 <=> 1); Set volume
  • volumeControl.getVolume(); Get volume, returns a promise with current volume

volumeControl.init options parameter

  • volume: Set volume during initialization
  • hideVolumeNotification: (iOS only) hides the volume UI during volume change

Quick Example

var options = {volume: 0.5, hideVolumeNotification:true};

//init
volumeControl.init(options, function(vol){
  console.log("Volume changed" , vol);
});

cordova ios volumedownbutton / volumeupbutton events polyfill

document.addEventListener("deviceready", () => {
  var curVolume = null
  if (cordova.platformId === 'ios') {
    volumeControl.init({}, (vol) => {
      if (curVolume !== null) {
        if (curVolume <= vol) {
          document.dispatchEvent(new Event('volumedownbutton'))
        } else {
          document.dispatchEvent(new Event('volumeupbutton'))
        }
      }
      curVolume = vol
      console.log("Volume changed", vol);
    });
  }
}, false);

setVolume() example

volumeControl.setVolume(0.5);

getVolume() example

volumeControl.getVolume().then((vol) => {
  console.log("Volume" , vol);
});