From ca5e141b5b261e1d58934a63681a8ec45ca0c6a3 Mon Sep 17 00:00:00 2001 From: macdonst Date: Fri, 27 May 2011 01:50:45 +0800 Subject: [PATCH] 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. --- framework/src/com/phonegap/AudioHandler.java | 14 ++++++------ framework/src/com/phonegap/AudioPlayer.java | 23 +++++++++++++------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/framework/src/com/phonegap/AudioHandler.java b/framework/src/com/phonegap/AudioHandler.java index cd16b1b9..9673b076 100755 --- a/framework/src/com/phonegap/AudioHandler.java +++ b/framework/src/com/phonegap/AudioHandler.java @@ -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); diff --git a/framework/src/com/phonegap/AudioPlayer.java b/framework/src/com/phonegap/AudioPlayer.java index f0b3da14..8f84a90e 100755 --- a/framework/src/com/phonegap/AudioPlayer.java +++ b/framework/src/com/phonegap/AudioPlayer.java @@ -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).