Changing Media class to return seconds

The media commands getCurrentPosition() and getDuration() will now return seconds (float) instead of milliseconds to line up with iOS and the docs.
This commit is contained in:
macdonst 2011-05-27 01:50:45 +08:00
parent 1d79b6617b
commit ca5e141b5b
2 changed files with 22 additions and 15 deletions

View File

@ -73,12 +73,12 @@ public class AudioHandler extends Plugin {
this.stopPlayingAudio(args.getString(0));
}
else if (action.equals("getCurrentPositionAudio")) {
long l = this.getCurrentPositionAudio(args.getString(0));
return new PluginResult(status, l);
float f = this.getCurrentPositionAudio(args.getString(0));
return new PluginResult(status, f);
}
else if (action.equals("getDurationAudio")) {
long l = this.getDurationAudio(args.getString(0), args.getString(1));
return new PluginResult(status, l);
float f = this.getDurationAudio(args.getString(0), args.getString(1));
return new PluginResult(status, f);
}
else if (action.equals("release")) {
boolean b = this.release(args.getString(0));
@ -230,10 +230,10 @@ public class AudioHandler extends Plugin {
* @param id The id of the audio player
* @return position in msec
*/
public long getCurrentPositionAudio(String id) {
public float getCurrentPositionAudio(String id) {
AudioPlayer audio = this.players.get(id);
if (audio != null) {
return(audio.getCurrentPosition());
return(audio.getCurrentPosition()/1000.0f);
}
return -1;
}
@ -245,7 +245,7 @@ public class AudioHandler extends Plugin {
* @param file The name of the audio file.
* @return The duration in msec.
*/
public long getDurationAudio(String id, String file) {
public float getDurationAudio(String id, String file) {
// Get audio file
AudioPlayer audio = this.players.get(id);

View File

@ -15,6 +15,7 @@ import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaRecorder;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Environment;
/**
* This class implements the audio playback and recording capabilities used by PhoneGap.
@ -53,7 +54,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
private String id; // The id of this player (used to identify Media object in JavaScript)
private int state = MEDIA_NONE; // State of recording or playback
private String audioFile = null; // File name to play or record to
private long duration = -1; // Duration of audio
private float duration = -1; // Duration of audio
private MediaRecorder recorder = null; // Audio recording object
private String tempFile = null; // Temporary recording file name
@ -70,10 +71,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
public AudioPlayer(AudioHandler handler, String id) {
this.handler = handler;
this.id = id;
// YES, I know this is bad, but I can't do it the right way because Google didn't have the
// foresight to add android.os.environment.getExternalDataDirectory until Android 2.2
this.tempFile = "/sdcard/tmprecording.mp3";
this.tempFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmprecording.mp3";
}
/**
@ -209,7 +207,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
this.mPlayer.prepare();
// Get duration
this.duration = this.mPlayer.getDuration();
this.duration = getDurationInSeconds();
}
}
catch (Exception e) {
@ -319,7 +317,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
* -1=can't be determined
* -2=not allowed
*/
public long getDuration(String file) {
public float getDuration(String file) {
// Can't get duration of recording
if (this.recorder != null) {
@ -362,7 +360,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
}
// Save off duration
this.duration = this.mPlayer.getDuration();
this.duration = getDurationInSeconds();
this.prepareOnly = false;
// Send status notification to JavaScript
@ -370,6 +368,15 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
}
/**
* By default Android returns the length of audio in mills but we want seconds
*
* @return length of clip in seconds
*/
private float getDurationInSeconds() {
return (this.mPlayer.getDuration() / 1000.0f);
}
/**
* Callback to be invoked when there has been an error during an asynchronous operation
* (other errors will throw exceptions at method call time).