diff --git a/plugin.xml b/plugin.xml index f5f6ec1..e80658a 100644 --- a/plugin.xml +++ b/plugin.xml @@ -32,5 +32,15 @@ + + + + + + + + + + diff --git a/src/android/VolumeSlider.java b/src/android/VolumeSlider.java new file mode 100755 index 0000000..32ff9dc --- /dev/null +++ b/src/android/VolumeSlider.java @@ -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); + } +} \ No newline at end of file diff --git a/src/ios/VolumeSlider.h b/src/ios/VolumeSlider.h index fa80e2a..14269f8 100644 --- a/src/ios/VolumeSlider.h +++ b/src/ios/VolumeSlider.h @@ -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 diff --git a/src/ios/VolumeSlider.m b/src/ios/VolumeSlider.m index f984e09..845defc 100644 --- a/src/ios/VolumeSlider.m +++ b/src/ios/VolumeSlider.m @@ -14,6 +14,9 @@ @implementation VolumeSlider @synthesize mpVolumeViewParentView, myVolumeView, callbackId; +float userVolume = 0.2; +UISlider* volumeViewSlider = nil; + #ifndef __IPHONE_3_0 @synthesize webView; #endif @@ -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]; @@ -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; @@ -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 @@ -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 diff --git a/www/VolumeSlider.js b/www/VolumeSlider.js index c0c579f..eb0ad6d 100644 --- a/www/VolumeSlider.js +++ b/www/VolumeSlider.js @@ -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]); @@ -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", []); + } };