Port AudioHandler and AudioPlayer to CordovaPlugin.

Had to adapt AudioPlayer because CordovaPlugin doesn't define
sendJavascript.
This commit is contained in:
Braden Shepherdson 2012-10-11 11:09:12 -04:00
parent e1dea5b4d3
commit 10510484b5
2 changed files with 35 additions and 41 deletions

View File

@ -18,12 +18,14 @@
*/ */
package org.apache.cordova; package org.apache.cordova;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import android.content.Context; import android.content.Context;
import android.media.AudioManager; import android.media.AudioManager;
import java.util.ArrayList; import java.util.ArrayList;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult; import org.apache.cordova.api.PluginResult;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
@ -40,7 +42,7 @@ import java.util.HashMap;
* android_asset: file name must start with /android_asset/sound.mp3 * android_asset: file name must start with /android_asset/sound.mp3
* sdcard: file name is just sound.mp3 * sdcard: file name is just sound.mp3
*/ */
public class AudioHandler extends Plugin { public class AudioHandler extends CordovaPlugin {
public static String TAG = "AudioHandler"; public static String TAG = "AudioHandler";
HashMap<String, AudioPlayer> players; // Audio player object HashMap<String, AudioPlayer> players; // Audio player object
@ -58,10 +60,10 @@ public class AudioHandler extends Plugin {
* Executes the request and returns PluginResult. * Executes the request and returns PluginResult.
* @param action The action to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the plugin. * @param args JSONArry of arguments for the plugin.
* @param callbackId The callback id used when calling back into JavaScript. * @param callbackContext The callback context used when calling back into JavaScript.
* @return A PluginResult object with a status and message. * @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args, String callbackId) { public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
PluginResult.Status status = PluginResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";
@ -91,11 +93,13 @@ public class AudioHandler extends Plugin {
} }
} else if (action.equals("getCurrentPositionAudio")) { } else if (action.equals("getCurrentPositionAudio")) {
float f = this.getCurrentPositionAudio(args.getString(0)); float f = this.getCurrentPositionAudio(args.getString(0));
return new PluginResult(status, f); callbackContext.sendPluginResult(new PluginResult(status, f));
return true;
} }
else if (action.equals("getDurationAudio")) { else if (action.equals("getDurationAudio")) {
float f = this.getDurationAudio(args.getString(0), args.getString(1)); float f = this.getDurationAudio(args.getString(0), args.getString(1));
return new PluginResult(status, f); callbackContext.sendPluginResult(new PluginResult(status, f));
return true;
} }
else if (action.equals("create")) { else if (action.equals("create")) {
String id = args.getString(0); String id = args.getString(0);
@ -105,29 +109,21 @@ public class AudioHandler extends Plugin {
} }
else if (action.equals("release")) { else if (action.equals("release")) {
boolean b = this.release(args.getString(0)); boolean b = this.release(args.getString(0));
return new PluginResult(status, b); callbackContext.sendPluginResult(new PluginResult(status, b));
} return true;
return new PluginResult(status, result);
} catch (JSONException e) {
e.printStackTrace();
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
} }
else { // Unrecognized action.
return false;
} }
/** callbackContext.sendPluginResult(new PluginResult(status, result));
* Identifies if action to be executed returns a value and should be run synchronously. } catch (JSONException e) {
* @param action The action to execute e.printStackTrace();
* @return T=returns value callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
*/
public boolean isSynch(String action) {
if (action.equals("getCurrentPositionAudio")) {
return true;
} }
else if (action.equals("getDurationAudio")) {
return true; return true;
} }
return false;
}
/** /**
* Stop all audio players and recorders. * Stop all audio players and recorders.

View File

@ -31,8 +31,6 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import org.apache.cordova.AudioPlayer.MODE;
/** /**
* This class implements the audio playback and recording capabilities used by Cordova. * This class implements the audio playback and recording capabilities used by Cordova.
* It is called by the AudioHandler Cordova class. * It is called by the AudioHandler Cordova class.
@ -135,7 +133,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
switch (this.mode) { switch (this.mode) {
case PLAY: case PLAY:
Log.d(LOG_TAG, "AudioPlayer Error: Can't record in play mode."); Log.d(LOG_TAG, "AudioPlayer Error: Can't record in play mode.");
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});"); this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
break; break;
case NONE: case NONE:
this.audioFile = file; this.audioFile = file;
@ -153,11 +151,11 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});"); this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
break; break;
case RECORD: case RECORD:
Log.d(LOG_TAG, "AudioPlayer Error: Already recording."); Log.d(LOG_TAG, "AudioPlayer Error: Already recording.");
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});"); this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
} }
} }
@ -223,7 +221,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
if (this.readyPlayer(this.audioFile)) { if (this.readyPlayer(this.audioFile)) {
this.player.seekTo(milliseconds); this.player.seekTo(milliseconds);
Log.d(LOG_TAG, "Send a onStatus update for the new seek"); Log.d(LOG_TAG, "Send a onStatus update for the new seek");
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_POSITION + ", " + milliseconds / 1000.0f + ");"); this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_POSITION + ", " + milliseconds / 1000.0f + ");");
} }
else { else {
this.seekOnPrepared = milliseconds; this.seekOnPrepared = milliseconds;
@ -242,7 +240,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
} }
else { else {
Log.d(LOG_TAG, "AudioPlayer Error: pausePlaying() called during invalid state: " + this.state.ordinal()); Log.d(LOG_TAG, "AudioPlayer Error: pausePlaying() called during invalid state: " + this.state.ordinal());
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_NONE_ACTIVE + "});"); this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_NONE_ACTIVE + "});");
} }
} }
@ -258,7 +256,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
} }
else { else {
Log.d(LOG_TAG, "AudioPlayer Error: stopPlaying() called during invalid state: " + this.state.ordinal()); Log.d(LOG_TAG, "AudioPlayer Error: stopPlaying() called during invalid state: " + this.state.ordinal());
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_NONE_ACTIVE + "});"); this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_NONE_ACTIVE + "});");
} }
} }
@ -280,7 +278,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
public long getCurrentPosition() { public long getCurrentPosition() {
if ((this.state == STATE.MEDIA_RUNNING) || (this.state == STATE.MEDIA_PAUSED)) { if ((this.state == STATE.MEDIA_RUNNING) || (this.state == STATE.MEDIA_PAUSED)) {
int curPos = this.player.getCurrentPosition(); int curPos = this.player.getCurrentPosition();
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_POSITION + ", " + curPos / 1000.0f + ");"); this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_POSITION + ", " + curPos / 1000.0f + ");");
return curPos; return curPos;
} }
else { else {
@ -359,7 +357,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
this.prepareOnly = true; this.prepareOnly = true;
// Send status notification to JavaScript // Send status notification to JavaScript
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_DURATION + "," + this.duration + ");"); this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_DURATION + "," + this.duration + ");");
} }
/** /**
@ -387,7 +385,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
this.player.release(); this.player.release();
// Send error notification to JavaScript // Send error notification to JavaScript
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', { \"code\":" + arg1 + "});"); this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', { \"code\":" + arg1 + "});");
return false; return false;
} }
@ -398,7 +396,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
*/ */
private void setState(STATE state) { private void setState(STATE state) {
if (this.state != state) { if (this.state != state) {
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_STATE + ", " + state.ordinal() + ");"); this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_STATE + ", " + state.ordinal() + ");");
} }
this.state = state; this.state = state;
} }
@ -411,7 +409,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
private void setMode(MODE mode) { private void setMode(MODE mode) {
if (this.mode != mode) { if (this.mode != mode) {
//mode is not part of the expected behavior, so no notification //mode is not part of the expected behavior, so no notification
//this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_STATE + ", " + mode + ");"); //this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_STATE + ", " + mode + ");");
} }
this.mode = mode; this.mode = mode;
} }
@ -447,7 +445,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
break; break;
case RECORD: case RECORD:
Log.d(LOG_TAG, "AudioPlayer Error: Can't play in record mode."); Log.d(LOG_TAG, "AudioPlayer Error: Can't play in record mode.");
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});"); this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
return false; //player is not ready return false; //player is not ready
} }
return true; return true;
@ -468,7 +466,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
try { try {
this.loadAudioFile(file); this.loadAudioFile(file);
} catch (Exception e) { } catch (Exception e) {
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});"); this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
} }
return false; return false;
case MEDIA_LOADING: case MEDIA_LOADING:
@ -493,14 +491,14 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
try { try {
this.loadAudioFile(file); this.loadAudioFile(file);
} catch (Exception e) { } catch (Exception e) {
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});"); this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
} }
//if we had to prepare= the file, we won't be in the correct state for playback //if we had to prepare= the file, we won't be in the correct state for playback
return false; return false;
} }
default: default:
Log.d(LOG_TAG, "AudioPlayer Error: startPlaying() called during invalid state: " + this.state); Log.d(LOG_TAG, "AudioPlayer Error: startPlaying() called during invalid state: " + this.state);
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});"); this.handler.webView.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
} }
} }
return false; return false;
@ -526,7 +524,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
else { else {
if (file.startsWith("/android_asset/")) { if (file.startsWith("/android_asset/")) {
String f = file.substring(15); String f = file.substring(15);
android.content.res.AssetFileDescriptor fd = this.handler.ctx.getActivity().getAssets().openFd(f); android.content.res.AssetFileDescriptor fd = this.handler.cordova.getActivity().getAssets().openFd(f);
this.player.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength()); this.player.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
} }
else { else {