Add position callback and do some optimization of audio player.

This commit is contained in:
Bryce Curtis 2010-09-10 11:38:23 -05:00
parent 5c20ba57e9
commit 863807a7a5
2 changed files with 43 additions and 66 deletions

View File

@ -1,28 +1,3 @@
com.phonegap.AudioHandlerProxy = function() {
this.className = "com.phonegap.AudioHandler";
};
com.phonegap.AudioHandlerProxy.prototype.startRecordingAudio = function(id, file) {
return PhoneGap.exec(this.className, "startRecordingAudio", [id, file]);
};
com.phonegap.AudioHandlerProxy.prototype.stopRecordingAudio = function(id) {
return PhoneGap.exec(this.className, "stopRecordingAudio", [id]);
};
com.phonegap.AudioHandlerProxy.prototype.startPlayingAudio = function(id, file) {
return PhoneGap.exec(this.className, "startPlayingAudio", [id, file]);
};
com.phonegap.AudioHandlerProxy.prototype.pausePlayingAudio = function(id) {
return PhoneGap.exec(this.className, "pausePlayingAudio", [id]);
};
com.phonegap.AudioHandlerProxy.prototype.stopPlayingAudio = function(id) {
return PhoneGap.exec(this.className, "stopPlayingAudio", [id]);
};
com.phonegap.AudioHandlerProxy.prototype.getCurrentPositionAudio = function(id) {
return PhoneGap.exec(this.className, "getCurrentPositionAudio", [id]);
};
com.phonegap.AudioHandlerProxy.prototype.getDurationAudio = function(id, file) {
return PhoneGap.exec(this.className, "getDurationAudio", [id, file]);
};
com.phonegap.AudioHandler = new com.phonegap.AudioHandlerProxy();
/**
* List of media objects.
@ -88,8 +63,10 @@ PhoneGap.Media.onStatus = function(id, msg, value) {
* errorCallback(int errorCode) - OPTIONAL
* @param statusCallback The callback to be called when media status has changed.
* statusCallback(int statusCode) - OPTIONAL
* @param positionCallback The callback to be called when media position has changed.
* positionCallback(long position) - OPTIONAL
*/
Media = function(src, successCallback, errorCallback, statusCallback) {
Media = function(src, successCallback, errorCallback, statusCallback, positionCallback) {
// successCallback optional
if (successCallback && (typeof successCallback != "function")) {
@ -109,19 +86,27 @@ Media = function(src, successCallback, errorCallback, statusCallback) {
return;
}
// statusCallback optional
if (positionCallback && (typeof positionCallback != "function")) {
console.log("Media Error: positionCallback is not a function");
return;
}
this.id = PhoneGap.createUUID();
PhoneGap.mediaObjects[this.id] = this;
this.src = src;
this.successCallback = successCallback;
this.errorCallback = errorCallback;
this.statusCallback = statusCallback;
this.positionCallback = positionCallback;
this._duration = -1;
this._position = -1;
};
// Media messages
Media.MEDIA_STATE = 1;
Media.MEDIA_DURATION = 2;
Media.MEDIA_ERROR = 3;
Media.MEDIA_ERROR = 9;
// Media states
Media.MEDIA_NONE = 0;
@ -150,21 +135,21 @@ MediaError.MEDIA_ERR_NONE_SUPPORTED = 4;
* Start or resume playing audio file.
*/
Media.prototype.play = function() {
com.phonegap.AudioHandler.startPlayingAudio(this.id, this.src);
PhoneGap.execAsync(null, null, "com.phonegap.AudioHandler", "startPlayingAudio", [this.id, this.src]);
};
/**
* Stop playing audio file.
*/
Media.prototype.stop = function() {
com.phonegap.AudioHandler.stopPlayingAudio(this.id);
return PhoneGap.execAsync(null, null, "com.phonegap.AudioHandler", "stopPlayingAudio", [this.id]);
};
/**
* Pause playing audio file.
*/
Media.prototype.pause = function() {
com.phonegap.AudioHandler.pausePlayingAudio(this.id);
PhoneGap.execAsync(null, null, "com.phonegap.AudioHandler", "pausePlayingAudio", [this.id]);
};
/**
@ -182,21 +167,21 @@ Media.prototype.getDuration = function() {
*
* @return
*/
Media.prototype.getCurrentPosition = function() {
return com.phonegap.AudioHandler.getCurrentPositionAudio(this.id);
Media.prototype.getCurrentPosition = function(success, fail) {
PhoneGap.execAsync(success, fail, "com.phonegap.AudioHandler", "getCurrentPositionAudio", [this.id]);
};
/**
* Start recording audio file.
*/
Media.prototype.startRecord = function() {
com.phonegap.AudioHandler.startRecordingAudio(this.id, this.src);
PhoneGap.execAsync(null, null, "com.phonegap.AudioHandler", "startRecordingAudio", [this.id, this.src]);
};
/**
* Stop recording audio file.
*/
Media.prototype.stopRecord = function() {
com.phonegap.AudioHandler.stopRecordingAudio(this.id);
PhoneGap.execAsync(null, null, "com.phonegap.AudioHandler", "stopRecordingAudio", [this.id]);
};

View File

@ -30,7 +30,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
// AudioPlayer message ids
private static int MEDIA_STATE = 1;
private static int MEDIA_DURATION = 2;
private static int MEDIA_ERROR = 3;
private static int MEDIA_ERROR = 9;
// AudioPlayer error codes
private static int MEDIA_ERROR_PLAY_MODE_SET = 1;
@ -109,8 +109,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
try {
this.recorder.prepare();
this.recorder.start();
this.state = MEDIA_RUNNING;
this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_STATE+", "+this.state+");");
this.setState(MEDIA_RUNNING);
return;
} catch (IllegalStateException e) {
e.printStackTrace();
@ -144,11 +143,8 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
if (this.recorder != null) {
try{
if (this.state == MEDIA_RUNNING) {
this.state = MEDIA_STOPPED;
this.recorder.stop();
// Send status notification to JavaScript
this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_STATE+", "+this.state+");");
this.setState(MEDIA_STOPPED);
}
this.moveFile(this.audioFile);
}
@ -205,10 +201,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
this.duration = this.mPlayer.getDuration();
}
this.mPlayer.setOnPreparedListener(this);
this.state = MEDIA_STARTING;
// Send status notification to JavaScript
this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_STATE+", "+this.state+");");
this.setState(MEDIA_STARTING);
}
catch (Exception e) {
e.printStackTrace();
@ -222,10 +215,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
// If player has been paused, then resume playback
if ((this.state == MEDIA_PAUSED) || (this.state == MEDIA_STARTING)) {
this.mPlayer.start();
this.state = MEDIA_RUNNING;
// Send status notification to JavaScript
this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_STATE+", "+this.state+");");
this.setState(MEDIA_RUNNING);
}
else {
System.out.println("AudioPlayer Error: startPlaying() called during invalid state: "+this.state);
@ -242,10 +232,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
// If playing, then pause
if (this.state == MEDIA_RUNNING) {
this.mPlayer.pause();
this.state = MEDIA_PAUSED;
// Send status notification to JavaScript
this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_STATE+", "+this.state+");");
this.setState(MEDIA_PAUSED);
}
else {
System.out.println("AudioPlayer Error: pausePlaying() called during invalid state: "+this.state);
@ -258,11 +245,8 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
*/
public void stopPlaying() {
if ((this.state == MEDIA_RUNNING) || (this.state == MEDIA_PAUSED)) {
this.state = MEDIA_STOPPED;
this.mPlayer.stop();
// Send status notification to JavaScript
this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_STATE+", "+this.state+");");
this.setState(MEDIA_STOPPED);
}
else {
System.out.println("AudioPlayer Error: stopPlaying() called during invalid state: "+this.state);
@ -276,10 +260,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
* @param mPlayer The MediaPlayer that reached the end of the file
*/
public void onCompletion(MediaPlayer mPlayer) {
this.state = MEDIA_STOPPED;
// Send status notification to JavaScript
this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_STATE+", "+this.state+");");
this.setState(MEDIA_STOPPED);
}
/**
@ -359,10 +340,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
this.mPlayer.start();
// Set player init flag
this.state = MEDIA_RUNNING;
// Send status notification to JavaScript
this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_STATE+", "+this.state+");");
this.setState(MEDIA_RUNNING);
}
// Save off duration
@ -392,5 +370,19 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
// Send error notification to JavaScript
this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+arg1+");");
return false;
}
}
/**
* Set the state and send it to JavaScript.
*
* @param state
*/
private void setState(int state) {
if (this.state != state) {
this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_STATE+", "+this.state+");");
}
this.state = state;
}
}