Repository: cordova-plugin-media
Updated Branches:
  refs/heads/master 52b547f90 -> 27d24bbda


CB-6153 android: Make volume buttons control music stream while any audio 
players are created


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/commit/27d24bbd
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/tree/27d24bbd
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/diff/27d24bbd

Branch: refs/heads/master
Commit: 27d24bbda74675f2302e5ce160661f3e59d022f4
Parents: 52b547f
Author: Andrew Grieve <[email protected]>
Authored: Thu Nov 27 10:31:29 2014 -0500
Committer: Andrew Grieve <[email protected]>
Committed: Thu Nov 27 10:34:20 2014 -0500

----------------------------------------------------------------------
 src/android/AudioHandler.java | 65 ++++++++++++++++++++------------------
 1 file changed, 35 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/blob/27d24bbd/src/android/AudioHandler.java
----------------------------------------------------------------------
diff --git a/src/android/AudioHandler.java b/src/android/AudioHandler.java
index 45e6837..e6c766b 100644
--- a/src/android/AudioHandler.java
+++ b/src/android/AudioHandler.java
@@ -49,6 +49,7 @@ public class AudioHandler extends CordovaPlugin {
     public static String TAG = "AudioHandler";
     HashMap<String, AudioPlayer> players;      // Audio player object
     ArrayList<AudioPlayer> pausedForPhone;     // Audio players that were 
paused when phone call came in
+    private int origVolumeStream = -1;
 
     /**
      * Constructor.
@@ -122,8 +123,7 @@ public class AudioHandler extends CordovaPlugin {
         else if (action.equals("create")) {
             String id = args.getString(0);
             String src = FileHelper.stripFileProtocol(args.getString(1));
-            AudioPlayer audio = new AudioPlayer(this, id, src);
-            this.players.put(id, audio);
+            getOrCreatePlayer(id, src);
         }
         else if (action.equals("release")) {
             boolean b = this.release(args.getString(0));
@@ -197,16 +197,30 @@ public class AudioHandler extends CordovaPlugin {
     // LOCAL METHODS
     
//--------------------------------------------------------------------------
 
+    private AudioPlayer getOrCreatePlayer(String id, String file) {
+        AudioPlayer ret = players.get(id);
+        if (ret == null) {
+            if (players.isEmpty()) {
+                onFirstPlayerCreated();
+            }
+            ret = new AudioPlayer(this, id, file);
+            players.put(id, ret);
+        }
+        return ret;
+    }
+
     /**
      * Release the audio player instance to save memory.
      * @param id                               The id of the audio player
      */
     private boolean release(String id) {
-        if (!this.players.containsKey(id)) {
+        AudioPlayer audio = players.remove(id);
+        if (audio == null) {
             return false;
         }
-        AudioPlayer audio = this.players.get(id);
-        this.players.remove(id);
+        if (players.isEmpty()) {
+            onLastPlayerReleased();
+        }
         audio.destroy();
         return true;
     }
@@ -217,11 +231,7 @@ public class AudioHandler extends CordovaPlugin {
      * @param file                             The name of the file
      */
     public void startRecordingAudio(String id, String file) {
-        AudioPlayer audio = this.players.get(id);
-        if ( audio == null) {
-            audio = new AudioPlayer(this, id, file);
-            this.players.put(id, audio);
-        }
+        AudioPlayer audio = getOrCreatePlayer(id, file);
         audio.startRecording(file);
     }
 
@@ -242,11 +252,7 @@ public class AudioHandler extends CordovaPlugin {
      * @param file                             The name of the audio file.
      */
     public void startPlayingAudio(String id, String file) {
-        AudioPlayer audio = this.players.get(id);
-        if (audio == null) {
-            audio = new AudioPlayer(this, id, file);
-            this.players.put(id, audio);
-        }
+        AudioPlayer audio = getOrCreatePlayer(id, file);
         audio.startPlaying(file);
     }
 
@@ -281,8 +287,6 @@ public class AudioHandler extends CordovaPlugin {
         AudioPlayer audio = this.players.get(id);
         if (audio != null) {
             audio.stopPlaying();
-            //audio.destroy();
-            //this.players.remove(id);
         }
     }
 
@@ -306,19 +310,8 @@ public class AudioHandler extends CordovaPlugin {
      * @return                                 The duration in msec.
      */
     public float getDurationAudio(String id, String file) {
-
-        // Get audio file
-        AudioPlayer audio = this.players.get(id);
-        if (audio != null) {
-            return (audio.getDuration(file));
-        }
-
-        // If not already open, then open the file
-        else {
-            audio = new AudioPlayer(this, id, file);
-            this.players.put(id, audio);
-            return (audio.getDuration(file));
-        }
+        AudioPlayer audio = getOrCreatePlayer(id, file);
+        return audio.getDuration(file);
     }
 
     /**
@@ -373,4 +366,16 @@ public class AudioHandler extends CordovaPlugin {
             System.out.println("AudioHandler.setVolume() Error: Unknown Audio 
Player " + id);
         }
     }
+
+    private void onFirstPlayerCreated() {
+        origVolumeStream = cordova.getActivity().getVolumeControlStream();
+        
cordova.getActivity().setVolumeControlStream(AudioManager.STREAM_MUSIC);
+    }
+
+    private void onLastPlayerReleased() {
+        if (origVolumeStream != -1) {
+            cordova.getActivity().setVolumeControlStream(origVolumeStream);
+            origVolumeStream = -1;
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to