From ad8086fab5780cb45111b09365bc7ce3afbbed8c Mon Sep 17 00:00:00 2001 From: Kevin Griffin Date: Tue, 9 Aug 2011 23:18:01 -0400 Subject: [PATCH 1/2] exposing volume control --- framework/assets/js/media.js | 7 ++++ framework/src/com/phonegap/AudioHandler.java | 40 ++++++++++++++------ framework/src/com/phonegap/AudioPlayer.java | 14 ++++++- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/framework/assets/js/media.js b/framework/assets/js/media.js index de955c66..f530025c 100755 --- a/framework/assets/js/media.js +++ b/framework/assets/js/media.js @@ -155,6 +155,13 @@ Media.prototype.release = function() { PhoneGap.exec(null, null, "Media", "release", [this.id]); }; +/** + * Adjust the volume. + */ +Media.prototype.setVolume = function(volume) { + PhoneGap.exec(null, null, "Media", "setVolume", [this.id, volume]); +}; + /** * List of media objects. * PRIVATE diff --git a/framework/src/com/phonegap/AudioHandler.java b/framework/src/com/phonegap/AudioHandler.java index 9673b076..e5409a8e 100755 --- a/framework/src/com/phonegap/AudioHandler.java +++ b/framework/src/com/phonegap/AudioHandler.java @@ -7,17 +7,15 @@ */ package com.phonegap; -import java.util.HashMap; -import java.util.Map.Entry; - +import android.content.Context; +import android.media.AudioManager; +import com.phonegap.api.Plugin; +import com.phonegap.api.PluginResult; import org.json.JSONArray; import org.json.JSONException; -import com.phonegap.api.Plugin; -import com.phonegap.api.PluginResult; - -import android.content.Context; -import android.media.AudioManager; +import java.util.HashMap; +import java.util.Map.Entry; /** * This class called by PhonegapActivity to play and record audio. @@ -71,8 +69,13 @@ public class AudioHandler extends Plugin { } else if (action.equals("stopPlayingAudio")) { this.stopPlayingAudio(args.getString(0)); - } - else if (action.equals("getCurrentPositionAudio")) { + } else if (action.equals("setVolume")) { + try { + this.setVolume(args.getString(0), Float.parseFloat(args.getString(1))); + } catch (NumberFormatException nfe) { + //no-op + } + } else if (action.equals("getCurrentPositionAudio")) { float f = this.getCurrentPositionAudio(args.getString(0)); return new PluginResult(status, f); } @@ -295,5 +298,20 @@ public class AudioHandler extends Plugin { else { return -1; } - } + } + + /** + * Set the volume for an audio device + * + * @param id The id of the audio player + * @param volume Volume to adjust to 0.0f - 1.0f + */ + public void setVolume(String id, float volume) { + AudioPlayer audio = this.players.get(id); + if (audio != null) { + audio.setVolume(volume); + } else { + System.out.println("AudioHandler.setVolume() Error: Unknown Audio Player " + id); + } + } } diff --git a/framework/src/com/phonegap/AudioPlayer.java b/framework/src/com/phonegap/AudioPlayer.java index 2d99fdbc..df90795a 100755 --- a/framework/src/com/phonegap/AudioPlayer.java +++ b/framework/src/com/phonegap/AudioPlayer.java @@ -7,8 +7,6 @@ */ package com.phonegap; -import java.io.File; -import java.io.IOException; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; @@ -18,6 +16,9 @@ import android.media.MediaRecorder; import android.os.Environment; import android.util.Log; +import java.io.File; +import java.io.IOException; + /** * This class implements the audio playback and recording capabilities used by PhoneGap. * It is called by the AudioHandler PhoneGap class. @@ -417,4 +418,13 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On this.state = state; } + + /** + * Set the volume for audio player + * + * @param volume + */ + public void setVolume(float volume) { + this.mPlayer.setVolume(volume, volume); + } } From 381d1615b40596683b15d81d0e8e209895cb88be Mon Sep 17 00:00:00 2001 From: Kevin Griffin Date: Tue, 9 Aug 2011 23:19:50 -0400 Subject: [PATCH 2/2] formatting --- framework/src/com/phonegap/AudioHandler.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/framework/src/com/phonegap/AudioHandler.java b/framework/src/com/phonegap/AudioHandler.java index e5409a8e..e7f77d90 100755 --- a/framework/src/com/phonegap/AudioHandler.java +++ b/framework/src/com/phonegap/AudioHandler.java @@ -70,12 +70,12 @@ public class AudioHandler extends Plugin { else if (action.equals("stopPlayingAudio")) { this.stopPlayingAudio(args.getString(0)); } else if (action.equals("setVolume")) { - try { - this.setVolume(args.getString(0), Float.parseFloat(args.getString(1))); - } catch (NumberFormatException nfe) { - //no-op - } - } else if (action.equals("getCurrentPositionAudio")) { + try { + this.setVolume(args.getString(0), Float.parseFloat(args.getString(1))); + } catch (NumberFormatException nfe) { + //no-op + } + } else if (action.equals("getCurrentPositionAudio")) { float f = this.getCurrentPositionAudio(args.getString(0)); return new PluginResult(status, f); }