Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/test_project
.idea
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# cordova-volume-control
# cordova-simple-volume
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is see you changed the name of the plugin. I'm assuming this is because another plugin has claimed the npm namespace. I'm open to changing this, but it would be good if releases of this package would be in sync with this repo.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't change the name I just folked a folk which had already changed the name? I have no preference on name

Control volume and Receive volume events (iOS /Android)

### Supported Platforms
Expand All @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pointing to wrong repo

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just for my testing on my folk. I will put everything to point to the main repo


### 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
Expand All @@ -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);
});
```
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong repo

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just for my testing on my folk. I will put everything to point to the main repo

},
"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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong repo

},
"homepage": "https://github.com/boedy/cordova-volume-control"
}
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="cordova-volume-control" version="0.0.1">
<name>Cordova volume control</name>
id="cordova-simple-volume" version="1.0.1">
<name>Cordova simple volume control</name>
<description>Control volume and Receive volume events (iOS /Android)</description>
<license>Apache 2.0</license>
<keywords>cordova,volume,ios,buttons</keywords>
Expand Down
46 changes: 31 additions & 15 deletions src/android/VolumeControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
29 changes: 14 additions & 15 deletions src/ios/VolumeControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

9 changes: 9 additions & 0 deletions src/ios/VolumeControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
16 changes: 12 additions & 4 deletions www/volumeControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iOS does not support es6 syntax. So arrow functions (=>) don't work.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats strange as the plugin works on my iPhone? I will remove and test without the arrow functions.

Copy link
Owner

@boedy boedy Aug 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah it seems it works on the newer iOS safari versions . It would be good to support older versions too though (e.g iPod touch 5th generation is on iOS 9).

return new Promise((resolve, reject) => {
cordova.exec((volume) => {
resolve(parseFloat(volume))
}, () => {
reject("error getting volume")
}, "VolumeControl", "getVolumeCommand");
})
}
};

};