2009-11-10 09:45:02 +08:00
|
|
|
package com.phonegap;
|
2009-04-02 07:56:43 +08:00
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2010-09-07 02:13:09 +08:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
2010-09-08 02:59:54 +08:00
|
|
|
import com.phonegap.api.Plugin;
|
|
|
|
import com.phonegap.api.PluginResult;
|
2010-09-07 02:13:09 +08:00
|
|
|
|
2009-04-02 07:56:43 +08:00
|
|
|
import android.content.Context;
|
2010-09-01 04:39:37 +08:00
|
|
|
import android.media.AudioManager;
|
2009-04-02 07:56:43 +08:00
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* This class called by DroidGap to play and record audio.
|
|
|
|
* The file can be local or over a network using http.
|
|
|
|
*
|
|
|
|
* Audio formats supported (tested):
|
|
|
|
* .mp3, .wav
|
|
|
|
*
|
|
|
|
* Local audio files must reside in one of two places:
|
|
|
|
* android_asset: file name must start with /android_asset/sound.mp3
|
|
|
|
* sdcard: file name is just sound.mp3
|
|
|
|
*/
|
2010-10-05 12:58:14 +08:00
|
|
|
public class AudioHandler extends Plugin {
|
2010-09-07 02:13:09 +08:00
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
HashMap<String,AudioPlayer> players; // Audio player object
|
2009-04-02 07:56:43 +08:00
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
2010-09-07 02:13:09 +08:00
|
|
|
public AudioHandler() {
|
2010-09-01 04:39:37 +08:00
|
|
|
this.players = new HashMap<String,AudioPlayer>();
|
2009-04-02 07:56:43 +08:00
|
|
|
}
|
|
|
|
|
2010-09-07 02:13:09 +08:00
|
|
|
/**
|
|
|
|
* Executes the request and returns CommandResult.
|
|
|
|
*
|
|
|
|
* @param action The command to execute.
|
|
|
|
* @param args JSONArry of arguments for the command.
|
|
|
|
* @return A CommandResult object with a status and message.
|
|
|
|
*/
|
2010-09-08 02:59:54 +08:00
|
|
|
public PluginResult execute(String action, JSONArray args) {
|
|
|
|
PluginResult.Status status = PluginResult.Status.OK;
|
2010-09-07 02:13:09 +08:00
|
|
|
String result = "";
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (action.equals("startRecordingAudio")) {
|
|
|
|
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), args.getString(1));
|
|
|
|
}
|
|
|
|
else if (action.equals("pausePlayingAudio")) {
|
|
|
|
this.pausePlayingAudio(args.getString(0));
|
|
|
|
}
|
|
|
|
else if (action.equals("stopPlayingAudio")) {
|
|
|
|
this.stopPlayingAudio(args.getString(0));
|
|
|
|
}
|
|
|
|
else if (action.equals("getCurrentPositionAudio")) {
|
|
|
|
long l = this.getCurrentPositionAudio(args.getString(0));
|
2010-09-08 02:59:54 +08:00
|
|
|
return new PluginResult(status, l);
|
2010-09-07 02:13:09 +08:00
|
|
|
}
|
|
|
|
else if (action.equals("getDurationAudio")) {
|
|
|
|
long l = this.getDurationAudio(args.getString(0), args.getString(1));
|
2010-09-08 02:59:54 +08:00
|
|
|
return new PluginResult(status, l);
|
2010-09-07 02:13:09 +08:00
|
|
|
}
|
2010-09-08 02:59:54 +08:00
|
|
|
return new PluginResult(status, result);
|
2010-09-07 02:13:09 +08:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2010-09-08 02:59:54 +08:00
|
|
|
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
2010-09-07 02:13:09 +08:00
|
|
|
}
|
|
|
|
}
|
2010-09-11 00:34:06 +08:00
|
|
|
|
|
|
|
/**
|
2010-09-14 00:01:44 +08:00
|
|
|
* Identifies if action to be executed returns a value and should be run synchronously.
|
2010-09-11 00:34:06 +08:00
|
|
|
*
|
|
|
|
* @param action The action to execute
|
|
|
|
* @return T=returns value
|
|
|
|
*/
|
2010-09-14 00:01:44 +08:00
|
|
|
public boolean isSynch(String action) {
|
2010-09-11 00:34:06 +08:00
|
|
|
if (action.equals("getCurrentPositionAudio")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (action.equals("getDurationAudio")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-09-07 02:13:09 +08:00
|
|
|
|
2010-06-09 07:30:20 +08:00
|
|
|
/**
|
2010-09-01 04:39:37 +08:00
|
|
|
* Stop all audio players and recorders.
|
|
|
|
*/
|
2010-09-04 06:24:55 +08:00
|
|
|
public void onDestroy() {
|
2010-09-01 04:39:37 +08:00
|
|
|
java.util.Set<Entry<String,AudioPlayer>> s = this.players.entrySet();
|
|
|
|
java.util.Iterator<Entry<String,AudioPlayer>> it = s.iterator();
|
|
|
|
while(it.hasNext()) {
|
|
|
|
Entry<String,AudioPlayer> entry = it.next();
|
|
|
|
AudioPlayer audio = entry.getValue();
|
|
|
|
audio.destroy();
|
|
|
|
}
|
|
|
|
this.players.clear();
|
|
|
|
}
|
2010-09-07 02:13:09 +08:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
// LOCAL METHODS
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* Start recording and save the specified file.
|
|
|
|
*
|
|
|
|
* @param id The id of the audio player
|
|
|
|
* @param file The name of the file
|
|
|
|
*/
|
|
|
|
public void startRecordingAudio(String id, String file) {
|
|
|
|
// If already recording, then just return;
|
|
|
|
if (this.players.containsKey(id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
AudioPlayer audio = new AudioPlayer(this, id);
|
|
|
|
this.players.put(id, audio);
|
2010-06-09 07:30:20 +08:00
|
|
|
audio.startRecording(file);
|
|
|
|
}
|
2010-09-01 04:39:37 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop recording and save to the file specified when recording started.
|
|
|
|
*
|
|
|
|
* @param id The id of the audio player
|
|
|
|
*/
|
|
|
|
public void stopRecordingAudio(String id) {
|
|
|
|
AudioPlayer audio = this.players.get(id);
|
|
|
|
if (audio != null) {
|
|
|
|
audio.stopRecording();
|
|
|
|
this.players.remove(id);
|
|
|
|
}
|
2010-06-09 07:30:20 +08:00
|
|
|
}
|
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* Start or resume playing audio file.
|
|
|
|
*
|
|
|
|
* @param id The id of the audio player
|
|
|
|
* @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);
|
|
|
|
this.players.put(id, audio);
|
|
|
|
}
|
2010-06-09 07:30:20 +08:00
|
|
|
audio.startPlaying(file);
|
|
|
|
}
|
2010-09-01 04:39:37 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pause playing.
|
|
|
|
*
|
|
|
|
* @param id The id of the audio player
|
|
|
|
*/
|
|
|
|
public void pausePlayingAudio(String id) {
|
|
|
|
AudioPlayer audio = this.players.get(id);
|
|
|
|
if (audio != null) {
|
|
|
|
audio.pausePlaying();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop playing the audio file.
|
|
|
|
*
|
|
|
|
* @param id The id of the audio player
|
|
|
|
*/
|
|
|
|
public void stopPlayingAudio(String id) {
|
|
|
|
AudioPlayer audio = this.players.get(id);
|
|
|
|
if (audio != null) {
|
|
|
|
audio.stopPlaying();
|
|
|
|
//audio.destroy();
|
|
|
|
//this.players.remove(id);
|
|
|
|
}
|
2010-06-09 07:30:20 +08:00
|
|
|
}
|
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* Get current position of playback.
|
|
|
|
*
|
|
|
|
* @param id The id of the audio player
|
|
|
|
* @return position in msec
|
|
|
|
*/
|
|
|
|
public long getCurrentPositionAudio(String id) {
|
|
|
|
AudioPlayer audio = this.players.get(id);
|
|
|
|
if (audio != null) {
|
|
|
|
return(audio.getCurrentPosition());
|
|
|
|
}
|
|
|
|
return -1;
|
2010-06-09 07:30:20 +08:00
|
|
|
}
|
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* Get the duration of the audio file.
|
|
|
|
*
|
|
|
|
* @param id The id of the audio player
|
|
|
|
* @param file The name of the audio file.
|
|
|
|
* @return The duration in msec.
|
|
|
|
*/
|
|
|
|
public long 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);
|
|
|
|
this.players.put(id, audio);
|
|
|
|
return(audio.getDuration(file));
|
|
|
|
}
|
2010-06-09 07:30:20 +08:00
|
|
|
}
|
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* Set the audio device to be used for playback.
|
|
|
|
*
|
|
|
|
* @param output 1=earpiece, 2=speaker
|
|
|
|
*/
|
|
|
|
public void setAudioOutputDevice(int output) {
|
2010-09-07 02:13:09 +08:00
|
|
|
AudioManager audiMgr = (AudioManager) this.ctx.getSystemService(Context.AUDIO_SERVICE);
|
2010-09-01 04:39:37 +08:00
|
|
|
if (output == 2) {
|
|
|
|
audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL);
|
|
|
|
}
|
|
|
|
else if (output == 1) {
|
|
|
|
audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
System.out.println("AudioHandler.setAudioOutputDevice() Error: Unknown output device.");
|
|
|
|
}
|
2010-06-09 07:30:20 +08:00
|
|
|
}
|
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* Get the audio device to be used for playback.
|
|
|
|
*
|
|
|
|
* @return 1=earpiece, 2=speaker
|
|
|
|
*/
|
|
|
|
public int getAudioOutputDevice() {
|
2010-09-07 02:13:09 +08:00
|
|
|
AudioManager audiMgr = (AudioManager) this.ctx.getSystemService(Context.AUDIO_SERVICE);
|
2010-09-01 04:39:37 +08:00
|
|
|
if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_EARPIECE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_SPEAKER) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return -1;
|
|
|
|
}
|
2010-06-09 07:30:20 +08:00
|
|
|
}
|
2009-04-02 07:56:43 +08:00
|
|
|
}
|