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/08/18 16:37:47 UTC

[GitHub] janpio closed pull request #24: Added parameter for additional httpHeaders in Javascript.

janpio closed pull request #24: Added parameter for additional httpHeaders in Javascript.
URL: https://github.com/apache/cordova-plugin-media/pull/24
 
 
   

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 45e68377..c49c3b2d 100644
--- a/src/android/AudioHandler.java
+++ b/src/android/AudioHandler.java
@@ -25,10 +25,12 @@ Licensed to the Apache Software Foundation (ASF) under one
 import android.content.Context;
 import android.media.AudioManager;
 import android.net.Uri;
+import android.util.Log;
 
 import java.util.ArrayList;
 
 import org.apache.cordova.PluginResult;
+import org.json.JSONObject;
 import org.json.JSONArray;
 import org.json.JSONException;
 import java.util.HashMap;
@@ -47,6 +49,7 @@ Licensed to the Apache Software Foundation (ASF) under one
 public class AudioHandler extends CordovaPlugin {
 
     public static String TAG = "AudioHandler";
+    private static final String LOG_TAG = "AudioHandler";
     HashMap<String, AudioPlayer> players;	// Audio player object
     ArrayList<AudioPlayer> pausedForPhone;     // Audio players that were paused when phone call came in
 
@@ -93,7 +96,8 @@ else if (action.equals("startPlayingAudio")) {
             } catch (IllegalArgumentException e) {
                 fileUriStr = target;
             }
-            this.startPlayingAudio(args.getString(0), FileHelper.stripFileProtocol(fileUriStr));
+            JSONObject httpHeaders = new JSONObject(args.getString(3));            
+            this.startPlayingAudio(args.getString(0), FileHelper.stripFileProtocol(fileUriStr), httpHeaders);
         }
         else if (action.equals("seekToAudio")) {
             this.seekToAudio(args.getString(0), args.getInt(1));
@@ -122,7 +126,10 @@ else if (action.equals("getDurationAudio")) {
         else if (action.equals("create")) {
             String id = args.getString(0);
             String src = FileHelper.stripFileProtocol(args.getString(1));
-            AudioPlayer audio = new AudioPlayer(this, id, src);
+            
+            JSONObject httpHeaders = new JSONObject(args.getString(3));
+            Context context = this.cordova.getActivity().getApplicationContext();
+            AudioPlayer audio = new AudioPlayer(this, id, src, httpHeaders, context);
             this.players.put(id, audio);
         }
         else if (action.equals("release")) {
@@ -219,7 +226,7 @@ private boolean release(String id) {
     public void startRecordingAudio(String id, String file) {
         AudioPlayer audio = this.players.get(id);
         if ( audio == null) {
-            audio = new AudioPlayer(this, id, file);
+            audio = new AudioPlayer(this, id, file, null, this.cordova.getActivity().getApplicationContext());
             this.players.put(id, audio);
         }
         audio.startRecording(file);
@@ -241,10 +248,16 @@ public void stopRecordingAudio(String id) {
      * @param id				The id of the audio player
      * @param file				The name of the audio file.
      */
-    public void startPlayingAudio(String id, String file) {
+    public void startPlayingAudio(String id, String file, JSONObject httpHeaders) {
         AudioPlayer audio = this.players.get(id);
         if (audio == null) {
-            audio = new AudioPlayer(this, id, file);
+        	if(httpHeaders != null)
+	            Log.d(LOG_TAG, "creating in play with httpHeaders: " + httpHeaders.toString());
+			else	           
+	            Log.d(LOG_TAG, "creating in play with null httpHeaders");
+	        
+	        Context context = this.cordova.getActivity().getApplicationContext();
+            audio = new AudioPlayer(this, id, file, httpHeaders, context);
             this.players.put(id, audio);
         }
         audio.startPlaying(file);
@@ -315,7 +328,8 @@ public float getDurationAudio(String id, String file) {
 
         // If not already open, then open the file
         else {
-            audio = new AudioPlayer(this, id, file);
+	        Context context = this.cordova.getActivity().getApplicationContext();
+            audio = new AudioPlayer(this, id, file, null, context);
             this.players.put(id, audio);
             return (audio.getDuration(file));
         }
diff --git a/src/android/AudioPlayer.java b/src/android/AudioPlayer.java
index 30ca1acb..0645bcc0 100644
--- a/src/android/AudioPlayer.java
+++ b/src/android/AudioPlayer.java
@@ -18,18 +18,26 @@ Licensed to the Apache Software Foundation (ASF) under one
 */
 package org.apache.cordova.media;
 
+import android.content.Context;
 import android.media.AudioManager;
 import android.media.MediaPlayer;
 import android.media.MediaPlayer.OnCompletionListener;
 import android.media.MediaPlayer.OnErrorListener;
 import android.media.MediaPlayer.OnPreparedListener;
 import android.media.MediaRecorder;
+import android.net.Uri;
 import android.os.Environment;
 import android.util.Log;
 
+import org.json.JSONObject;
+import org.json.JSONException;
+
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Iterator;
 
 /**
  * This class implements the audio playback and recording capabilities used by Cordova.
@@ -83,19 +91,23 @@ Licensed to the Apache Software Foundation (ASF) under one
     private MediaPlayer player = null;      // Audio player object
     private boolean prepareOnly = true;     // playback after file prepare flag
     private int seekOnPrepared = 0;     // seek to this location once media is prepared
-
+	private Map<String, String> httpHeaders; // additional http headers to send
+	private Context context = null;
+	
     /**
      * Constructor.
      *
      * @param handler           The audio handler object
      * @param id                The id of this audio player
      */
-    public AudioPlayer(AudioHandler handler, String id, String file) {
+    public AudioPlayer(AudioHandler handler, String id, String file, JSONObject passedHttpHeaders, Context context) {
         this.handler = handler;
         this.id = id;
         this.audioFile = file;
         this.recorder = new MediaRecorder();
-
+        this.context = context;       	
+   		this.httpHeaders = mapHttpHeaders(passedHttpHeaders);
+	        
         if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
             this.tempFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmprecording.3gp";
         } else {
@@ -104,6 +116,25 @@ public AudioPlayer(AudioHandler handler, String id, String file) {
 
     }
 
+	private Map<String,String> mapHttpHeaders(JSONObject passedHttpHeaders) {
+		Map<String, String> httpHeaders = null;
+		if(passedHttpHeaders != null)
+       	{
+       		httpHeaders = new HashMap();
+       		String headerName;
+			for (Iterator<String> iter = passedHttpHeaders.keys(); iter.hasNext(); ) {
+				headerName = iter.next();
+				try {
+					httpHeaders.put(headerName, passedHttpHeaders.getString(headerName));
+				}
+				catch(JSONException exception){
+					Log.d(LOG_TAG, "Exception in mapping httpHeaders: " + exception.toString());
+				}
+			}
+		}
+		return httpHeaders;
+	}
+
     /**
      * Destroy player and stop audio playing or recording.
      */
@@ -519,7 +550,13 @@ private boolean readyPlayer(String file) {
      */
     private void loadAudioFile(String file) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {
         if (this.isStreaming(file)) {
-            this.player.setDataSource(file);
+        	if(this.httpHeaders != null) {
+        		Uri uri = Uri.parse(file);
+        		this.player.setDataSource(this.context, uri, this.httpHeaders);
+        	}
+        	else {
+            	this.player.setDataSource(file);
+            }
             this.player.setAudioStreamType(AudioManager.STREAM_MUSIC);
             //if it's a streaming file, play mode is implied
             this.setMode(MODE.PLAY);
diff --git a/www/Media.js b/www/Media.js
index 083af362..da65acd8 100644
--- a/www/Media.js
+++ b/www/Media.js
@@ -36,8 +36,9 @@ var mediaObjects = {};
  *                                  errorCallback(int errorCode) - OPTIONAL
  * @param statusCallback        The callback to be called when media status has changed.
  *                                  statusCallback(int statusCode) - OPTIONAL
+ * @param httpHeaders			Additional httpHeader to send, as JSON object
  */
-var Media = function(src, successCallback, errorCallback, statusCallback) {
+var Media = function(src, successCallback, errorCallback, statusCallback, httpHeaders) {
     argscheck.checkArgs('SFFF', 'Media', arguments);
     this.id = utils.createUUID();
     mediaObjects[this.id] = this;
@@ -47,7 +48,8 @@ var Media = function(src, successCallback, errorCallback, statusCallback) {
     this.statusCallback = statusCallback;
     this._duration = -1;
     this._position = -1;
-    exec(null, this.errorCallback, "Media", "create", [this.id, this.src]);
+    this.httpHeaders = httpHeaders;
+    exec(null, this.errorCallback, "Media", "create", [this.id, this.src, httpHeaders]);
 };
 
 // Media messages
@@ -73,7 +75,7 @@ Media.get = function(id) {
  * Start or resume playing audio file.
  */
 Media.prototype.play = function(options) {
-    exec(null, null, "Media", "startPlayingAudio", [this.id, this.src, options]);
+    exec(null, null, "Media", "startPlayingAudio", [this.id, this.src, options, this.httpHeaders]);
 };
 
 /**


 

----------------------------------------------------------------
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