Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to change and then reset the ios master volume #8

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
10 changes: 10 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,15 @@
<source-file src="src/ios/VolumeSlider.m" />
<framework src="MediaPlayer.framework" weak="true" />
</platform>

<!-- android -->
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="VolumeSlider">
<param name="android-package" value="org.devgeeks.volumeslider.VolumeSlider" />
</feature>
</config-file>

<source-file src="src/android/VolumeSlider.java" target-dir="src/org/devgeeks/volumeslider" />
</platform>
</plugin>
149 changes: 149 additions & 0 deletions src/android/VolumeSlider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package org.devgeeks.volumeslider;

import android.content.Context;
import android.media.AudioManager;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.SeekBar;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONException;
import android.util.Log;

public class VolumeSlider extends CordovaPlugin {
private SeekBar volumeSeekBar;
private AudioManager audioManager;
private static final String TAG = "volume_slider";
double current_volume = 0.2;

private int cssToViewUnit(double size) {
return (int)Math.abs(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float)size,
cordova.getActivity().getResources().getDisplayMetrics()));
}

private double cssToViewUnitDouble(double size) {
return TypedValue.applyDimension(TypedValue.TYPE_NULL, (float)size,
cordova.getActivity().getResources().getDisplayMetrics());
}

@Override
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
if (action == null)
return false;
if (action.equals("createVolumeSlider")) {
final int x = cssToViewUnit(args.getDouble(0));
final int y = cssToViewUnit(args.getDouble(1));
final int width = cssToViewUnit(args.getDouble(2));
final int height = cssToViewUnit(args.getDouble(3));
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
createVolumeSlider(x, y, width, height);
}
});
return true;
}
if (action.equals("showVolumeSlider")) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
showVolumeSlider();
}
});
return true;
}
if (action.equals("hideVolumeSlider")) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
hideVolumeSlider();
}
});
return true;
}
if (action.equals("setVolumeSlider")) {
final double volume = cssToViewUnitDouble(args.getDouble(0));
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
setVolumeSlider(volume);
}
});
return true;
}
if (action.equals("resetVolumeSlider")) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
resetVolumeSlider();
}
});
return true;
}
return false;
}

private void bindVolumeSeekBarToAudioManager(SeekBar volumeSeekBar, final Context context)
{
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
volumeSeekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));

volumeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
}

private void createVolumeSlider(int x, int y, int width, int height) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
params.leftMargin = x;
params.topMargin = y;

Context context = cordova.getActivity();

volumeSeekBar = new SeekBar(context);
bindVolumeSeekBarToAudioManager(volumeSeekBar, context);

// Record current media volume to revert to with resetVolumeSlider()
current_volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Log.d(TAG, "CURRENT VOLUME = " + current_volume);
hideVolumeSlider();

RelativeLayout layout = new RelativeLayout(context);
layout.addView(volumeSeekBar, params);
ViewGroup container = (ViewGroup)cordova.getActivity().findViewById(android.R.id.content);
container.addView(layout);
}

private void showVolumeSlider() {
volumeSeekBar.setVisibility(View.VISIBLE);
}

private void hideVolumeSlider() {
volumeSeekBar.setVisibility(View.INVISIBLE);
}

private void setVolumeSlider(double volume) {
// We map volume to a range of 0.0 - 1.0
Log.d(TAG, "SET VOLUME TO: " + volume);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int)(volume * audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)), 0);
}

private void resetVolumeSlider() {
// Resetting back to initial volume
Log.d(TAG, "RESET VOLUME TO: " + current_volume);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int)(current_volume), 0);
}
}
2 changes: 2 additions & 0 deletions src/ios/VolumeSlider.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@
- (void)createVolumeSlider:(CDVInvokedUrlCommand *)command;
- (void)showVolumeSlider:(CDVInvokedUrlCommand *)command;
- (void)hideVolumeSlider:(CDVInvokedUrlCommand *)command;
- (void)setVolumeSlider:(CDVInvokedUrlCommand *)command;
- (void)resetVolumeSlider:(CDVInvokedUrlCommand *)command;

@end
45 changes: 43 additions & 2 deletions src/ios/VolumeSlider.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ @implementation VolumeSlider

@synthesize mpVolumeViewParentView, myVolumeView, callbackId;

float userVolume = 0.2;
UISlider* volumeViewSlider = nil;

#ifndef __IPHONE_3_0
@synthesize webView;
#endif
Expand All @@ -30,7 +33,8 @@ -(CDVPlugin*) initWithWebView:(UIWebView*)theWebView

- (void) createVolumeSlider:(CDVInvokedUrlCommand *)command
{
NSArray* arguments = [command arguments];
NSLog(@"In createVolumeSlider");
NSArray* arguments = [command arguments];

self.callbackId = command.callbackId;
NSUInteger argc = [arguments count];
Expand All @@ -40,7 +44,7 @@ - (void) createVolumeSlider:(CDVInvokedUrlCommand *)command
}

if (self.mpVolumeViewParentView != NULL) {
return;//already created, don't need to create it again
// return;//already created, don't need to create it again
}

CGFloat originx,originy,width;
Expand Down Expand Up @@ -68,12 +72,25 @@ - (void) createVolumeSlider:(CDVInvokedUrlCommand *)command
[[MPVolumeView alloc] initWithFrame: mpVolumeViewParentView.bounds];
[mpVolumeViewParentView addSubview: myVolumeView];
self.myVolumeView.showsVolumeSlider = NO;


volumeViewSlider = nil;
for (UIView *view in [self.myVolumeView subviews]){
if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
volumeViewSlider = (UISlider*)view;
NSLog(@"Found MPVolumeslider : %f" ,userVolume );
break;
}
}
userVolume = volumeViewSlider.value;

}

- (void)showVolumeSlider:(CDVInvokedUrlCommand *)command
{
self.myVolumeView.showsVolumeSlider = YES;
self.mpVolumeViewParentView.hidden = NO;

}

- (void)hideVolumeSlider:(CDVInvokedUrlCommand *)command
Expand All @@ -82,6 +99,30 @@ - (void)hideVolumeSlider:(CDVInvokedUrlCommand *)command
self.myVolumeView.showsVolumeSlider = NO;
}

- (void)setVolumeSlider:(CDVInvokedUrlCommand *)command
{
self.mpVolumeViewParentView.hidden = YES;
self.myVolumeView.showsVolumeSlider = NO;

NSArray* arguments = [command arguments];
NSUInteger argc = [arguments count];

if (argc < 1) { // at a minimum we need the value to be set...
return;
}
float setVolume = [[arguments objectAtIndex:0] floatValue];

[volumeViewSlider setValue:setVolume animated:NO];
[volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];

}

- (void)resetVolumeSlider:(CDVInvokedUrlCommand *)command
{
[volumeViewSlider setValue:userVolume animated:NO];
[volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];

}


@end
18 changes: 16 additions & 2 deletions www/VolumeSlider.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var exec = require('cordova/exec');
module.exports = {

/**
* Create a volume slider.
* Create a volume slider, and save the user's current set volume.
*/
createVolumeSlider : function(originx,originy,width,height) {
exec(null, null, "VolumeSlider","createVolumeSlider", [originx, originy, width, height]);
Expand All @@ -31,5 +31,19 @@ module.exports = {
*/
hideVolumeSlider : function() {
exec(null, null, "VolumeSlider","hideVolumeSlider", []);
}
},

/**
* Set the device's master volume
*/
setVolumeSlider : function(set_volume) {
exec(null, null, "VolumeSlider","setVolumeSlider", [set_volume]);
},

/**
* Reset the volume slider to the original user volume present uon creation of this slider
*/
resetVolumeSlider : function() {
exec(null, null, "VolumeSlider","resetVolumeSlider", []);
}
};