diff --git a/.gitignore b/.gitignore index ad794c5..6a3dae0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /test_project +.idea \ No newline at end of file diff --git a/README.md b/README.md index 9b22e30..4fff695 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# cordova-volume-control +# cordova-simple-volume Control volume and Receive volume events (iOS /Android) ### Supported Platforms @@ -8,13 +8,14 @@ Control volume and Receive volume events (iOS /Android) ## Installation - cordova plugin add https://github.com/boedy/cordova-volume-control --save + cordova plugin add https://github.com/online-data-intelligence/cordova-simple-volume ### 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 @@ -32,3 +33,35 @@ volumeControl.init(options, function(vol){ console.log("Volume changed" , vol); }); ``` + +### cordova ios volumedownbutton / volumeupbutton events polyfill +```js +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 +```js +volumeControl.setVolume(0.5); +``` + +### getVolume() example +```js +volumeControl.getVolume().then((vol) => { + console.log("Volume" , vol); +}); +``` \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..729c69f --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "cordova-simple-volume", + "version": "1.0.2", + "description": "Cordova plugin which notifies when the user presses the volume buttons of the device", + "cordova": { + "id": "cordova-simple-volume", + "platforms": [ "android", "ios" ] + }, + "repository": { + "type": "git", + "url": "git+https://github.com/online-data-intelligence/cordova-simple-volume.git" + }, + "keywords": [ "ecosystem:cordova", "cordova-android", "cordova-ios", "volume", "buttons" ], + "author": "Boudewijn Geiger", + "license": "Apache 2.0", + "bugs": { + "url": "https://github.com/online-data-intelligence/cordova-simple-volume/issues" + }, + "homepage": "https://github.com/boedy/cordova-volume-control" +} diff --git a/plugin.xml b/plugin.xml index 8a8b200..65ee278 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ - Cordova volume control + id="cordova-simple-volume" version="1.0.1"> + Cordova simple volume control Control volume and Receive volume events (iOS /Android) Apache 2.0 cordova,volume,ios,buttons diff --git a/src/android/VolumeControl.java b/src/android/VolumeControl.java index bd1e2ce..09ced6f 100644 --- a/src/android/VolumeControl.java +++ b/src/android/VolumeControl.java @@ -7,6 +7,8 @@ import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.PluginResult; +import org.apache.cordova.PluginResult.Status; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -20,49 +22,63 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo if (action.equals("initCommand")) { JSONObject options = data.getJSONObject(0); - if(options.has("volume") && this.observer == null){ + if (options.has("volume") && this.observer == null) { this.setVolume(options.getDouble("volume")); } this.init(callbackContext); return true; - } else if(action.equals("destroyCommand")){ + } else if (action.equals("destroyCommand")) { this.destroy(); return true; - } else if(action.equals("setVolumeCommand")){ + } else if (action.equals("setVolumeCommand")) { double volume = data.getDouble(0); this.setVolume(volume); return true; + } else if (action.equals("getVolumeCommand")) { + double volume = this.getVolume(); + final PluginResult result = new PluginResult(PluginResult.Status.OK, Double.toString(volume)); + callbackContext.sendPluginResult(result); + return true; } return false; } - public void init(CallbackContext callbackContext){ - if( this.observer != null){ + public void init(CallbackContext callbackContext) { + if (this.observer != null) { return; } - Context context=this.cordova.getActivity().getApplicationContext(); + Context context = this.cordova.getActivity().getApplicationContext(); this.observer = new VolumeObserver(callbackContext, context, null); - context.getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, this.observer); + context.getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, + this.observer); } - public void destroy(){ - if( this.observer != null){ - Context context=this.cordova.getActivity().getApplicationContext(); + public void destroy() { + if (this.observer != null) { + Context context = this.cordova.getActivity().getApplicationContext(); context.getContentResolver().unregisterContentObserver(this.observer); this.observer = null; } } - public void setVolume(double volume){ - Context context=this.cordova.getActivity().getApplicationContext(); - AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); + public void setVolume(double volume) { + Context context = this.cordova.getActivity().getApplicationContext(); + AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); - audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int)(maxVolume * volume) , 0); + audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int) (maxVolume * volume), 0); } -} + public double getVolume() { + Context context = this.cordova.getActivity().getApplicationContext(); + AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); + int streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); + double volume = (double) streamVolume / (double) maxVolume; + return volume; + } +} diff --git a/src/ios/VolumeControl.h b/src/ios/VolumeControl.h index fd223f5..6cb0571 100644 --- a/src/ios/VolumeControl.h +++ b/src/ios/VolumeControl.h @@ -3,23 +3,22 @@ @interface VolumeControl : CDVPlugin { - AVAudioSession* avSession; + AVAudioSession *avSession; UIView *volumeView; - NSString* callbackId; + NSString *callbackId; float launchVolume; - } -@property (nonatomic, strong) AVAudioSession* avSession; -@property (nonatomic, strong) CDVInvokedUrlCommand* callback; -@property (nonatomic, retain) UIView *volumeView; -@property (nonatomic) float launchVolume; -@property (strong) NSString* callbackId; -- (void) initCommand:(CDVInvokedUrlCommand*)command; -- (void) destroyCommand:(CDVInvokedUrlCommand*)command; -- (void) setVolumeCommand:(CDVInvokedUrlCommand*)command; -- (void) showVolumeNotifications; -- (void) hideVolumeNotifications; -- (void) setVolume:(float)level; +@property(nonatomic, strong) AVAudioSession *avSession; +@property(nonatomic, strong) CDVInvokedUrlCommand *callback; +@property(nonatomic, retain) UIView *volumeView; +@property(nonatomic) float launchVolume; +@property(strong) NSString *callbackId; +- (void)initCommand:(CDVInvokedUrlCommand *)command; +- (void)destroyCommand:(CDVInvokedUrlCommand *)command; +- (void)setVolumeCommand:(CDVInvokedUrlCommand *)command; +- (void)showVolumeNotifications; +- (void)hideVolumeNotifications; +- (void)setVolume:(float)level; +- (void)getVolumeCommand:(CDVInvokedUrlCommand *)command; @end - diff --git a/src/ios/VolumeControl.m b/src/ios/VolumeControl.m index 133a668..859a802 100644 --- a/src/ios/VolumeControl.m +++ b/src/ios/VolumeControl.m @@ -69,6 +69,15 @@ - (void)setVolumeCommand:(CDVInvokedUrlCommand *)command } +- (void)getVolumeCommand:(CDVInvokedUrlCommand *)command +{ + float volume = self.avSession.outputVolume; + + CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@(volume).stringValue]; + [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; +} + + - (void)startObservingVolumeChanges { [self.avSession setActive: true error:nil]; diff --git a/www/volumeControl.js b/www/volumeControl.js index 36cb6a1..8fb8883 100644 --- a/www/volumeControl.js +++ b/www/volumeControl.js @@ -2,11 +2,19 @@ module.exports = { init: function (options, successCallback, errorCallback) { cordova.exec(successCallback, errorCallback, "VolumeControl", "initCommand", [options]); }, - destroy: function(){ + destroy: function () { cordova.exec(null, null, "VolumeControl", "destroyCommand", null); }, - setVolume: function(volume){ + setVolume: function (volume) { cordova.exec(null, null, "VolumeControl", "setVolumeCommand", [volume]); + }, + getVolume: function () { + return new Promise((resolve, reject) => { + cordova.exec((volume) => { + resolve(parseFloat(volume)) + }, () => { + reject("error getting volume") + }, "VolumeControl", "getVolumeCommand"); + }) } -}; - +}; \ No newline at end of file