You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by GitBox <gi...@apache.org> on 2018/06/12 08:02:36 UTC

[GitHub] g-w closed pull request #6: Android: avoid creating new AudioPlayer on create, if it already exists

g-w closed pull request #6: Android: avoid creating new AudioPlayer on create, if it already exists
URL: https://github.com/apache/cordova-plugin-media/pull/6
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/android/AudioHandler.java b/src/android/AudioHandler.java
index 63d8580c..7919fa5e 100644
--- a/src/android/AudioHandler.java
+++ b/src/android/AudioHandler.java
@@ -24,6 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one
 import android.content.Context;
 import android.media.AudioManager;
 
+import java.lang.String;
 import java.util.ArrayList;
 
 import org.apache.cordova.PluginResult;
@@ -68,13 +69,13 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
         String result = "";
 
         if (action.equals("startRecordingAudio")) {
-            this.startRecordingAudio(args.getString(0), FileHelper.stripFileProtocol(args.getString(1)));
+            this.startRecordingAudio(args.getString(0), args.getString(1));
         }
         else if (action.equals("stopRecordingAudio")) {
             this.stopRecordingAudio(args.getString(0));
         }
         else if (action.equals("startPlayingAudio")) {
-            this.startPlayingAudio(args.getString(0), FileHelper.stripFileProtocol(args.getString(1)));
+            this.startPlayingAudio(args.getString(0), args.getString(1));
         }
         else if (action.equals("seekToAudio")) {
             this.seekToAudio(args.getString(0), args.getInt(1));
@@ -101,10 +102,7 @@ else if (action.equals("getDurationAudio")) {
             return true;
         }
         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);
+            createAudioPlayer(args.getString(0), args.getString(1));
         }
         else if (action.equals("release")) {
             boolean b = this.release(args.getString(0));
@@ -178,6 +176,24 @@ else if ("idle".equals(data)) {
     // LOCAL METHODS
     //--------------------------------------------------------------------------
 
+
+    /**
+     * Creates a new audio player and puts it into the player map
+     *
+     * The method checks if a audio player with the given id exists and ignores it if it does.
+     *
+     * @param id				The id of the audio player
+     * @param src               The src of of the audio to play
+     * @return                  The audio player created or the existing one
+     */
+    private void createAudioPlayer(String id, String src) {
+        AudioPlayer audio = this.players.get(id);
+        if (audio == null) {
+            AudioPlayer audio = new AudioPlayer(this, id, FileHelper.stripFileProtocol(src));
+            this.players.put(id, audio);
+        }
+    }
+
     /**
      * Release the audio player instance to save memory.
      * @param id				The id of the audio player
@@ -198,11 +214,7 @@ private boolean release(String id) {
      * @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 = createAudioPlayer(id, file);
         audio.startRecording(file);
     }
 
@@ -223,11 +235,7 @@ public void stopRecordingAudio(String id) {
      * @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 = createAudioPlayer(id, file);
         audio.startPlaying(file);
     }
 
@@ -296,8 +304,7 @@ public float getDurationAudio(String id, String file) {
 
         // If not already open, then open the file
         else {
-            audio = new AudioPlayer(this, id, file);
-            this.players.put(id, audio);
+            audio = createAudioPlayer(id, file);
             return (audio.getDuration(file));
         }
     }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org