CB-1211: Media record uses a .mp3 extension when it is a .3gp file

This commit is contained in:
macdonst 2012-08-08 15:34:26 -04:00
parent 9bac59b952
commit d35c913249

View File

@ -43,10 +43,10 @@ import org.apache.cordova.AudioPlayer.MODE;
* sdcard: file name is just sound.mp3 * sdcard: file name is just sound.mp3
*/ */
public class AudioPlayer implements OnCompletionListener, OnPreparedListener, OnErrorListener { public class AudioPlayer implements OnCompletionListener, OnPreparedListener, OnErrorListener {
// AudioPlayer modes // AudioPlayer modes
public enum MODE { NONE, PLAY, RECORD }; public enum MODE { NONE, PLAY, RECORD };
// AudioPlayer states // AudioPlayer states
public enum STATE { MEDIA_NONE, public enum STATE { MEDIA_NONE,
MEDIA_LOADING, MEDIA_LOADING,
@ -55,7 +55,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
MEDIA_PAUSED, MEDIA_PAUSED,
MEDIA_STOPPED MEDIA_STOPPED
}; };
private static final String LOG_TAG = "AudioPlayer"; private static final String LOG_TAG = "AudioPlayer";
// AudioPlayer message ids // AudioPlayer message ids
@ -70,7 +70,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
private static int MEDIA_ERR_NETWORK = 2; private static int MEDIA_ERR_NETWORK = 2;
private static int MEDIA_ERR_DECODE = 3; private static int MEDIA_ERR_DECODE = 3;
private static int MEDIA_ERR_NONE_SUPPORTED = 4; private static int MEDIA_ERR_NONE_SUPPORTED = 4;
private AudioHandler handler; // The AudioHandler object private AudioHandler handler; // The AudioHandler object
private String id; // The id of this player (used to identify Media object in JavaScript) private String id; // The id of this player (used to identify Media object in JavaScript)
private MODE mode = MODE.NONE; // Playback or Recording mode private MODE mode = MODE.NONE; // Playback or Recording mode
@ -81,26 +81,26 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
private MediaRecorder recorder = null; // Audio recording object private MediaRecorder recorder = null; // Audio recording object
private String tempFile = null; // Temporary recording file name private String tempFile = null; // Temporary recording file name
private MediaPlayer player = null; // Audio player object private MediaPlayer player = null; // Audio player object
private boolean prepareOnly = true; // playback after file prepare flag private boolean prepareOnly = true; // playback after file prepare flag
private int seekOnPrepared = 0; // seek to this location once media is prepared private int seekOnPrepared = 0; // seek to this location once media is prepared
/** /**
* Constructor. * Constructor.
* *
* @param handler The audio handler object * @param handler The audio handler object
* @param id The id of this audio player * @param id The id of this audio player
*/ */
public AudioPlayer(AudioHandler handler, String id, String file) { public AudioPlayer(AudioHandler handler, String id, String file) {
this.handler = handler; this.handler = handler;
this.id = id; this.id = id;
this.audioFile = file; this.audioFile = file;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
this.tempFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmprecording.mp3"; this.tempFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmprecording.3gp";
} else { } else {
this.tempFile = "/data/data/" + handler.cordova.getActivity().getPackageName() + "/cache/tmprecording.mp3"; this.tempFile = "/data/data/" + handler.cordova.getActivity().getPackageName() + "/cache/tmprecording.3gp";
} }
} }
@ -127,7 +127,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
/** /**
* Start recording the specified file. * Start recording the specified file.
* *
* @param file The name of the file * @param file The name of the file
*/ */
public void startRecording(String file) { public void startRecording(String file) {
@ -202,10 +202,10 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
//========================================================================== //==========================================================================
// Playback // Playback
//========================================================================== //==========================================================================
/** /**
* Start or resume playing audio file. * Start or resume playing audio file.
* *
* @param file The name of the audio file. * @param file The name of the audio file.
*/ */
public void startPlaying(String file) { public void startPlaying(String file) {
@ -264,8 +264,8 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
/** /**
* Callback to be invoked when playback of a media source has completed. * Callback to be invoked when playback of a media source has completed.
* *
* @param player The MediaPlayer that reached the end of the file * @param player The MediaPlayer that reached the end of the file
*/ */
public void onCompletion(MediaPlayer player) { public void onCompletion(MediaPlayer player) {
this.setState(STATE.MEDIA_STOPPED); this.setState(STATE.MEDIA_STOPPED);
@ -273,7 +273,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
/** /**
* Get current position of playback. * Get current position of playback.
* *
* @return position in msec or -1 if not playing * @return position in msec or -1 if not playing
*/ */
public long getCurrentPosition() { public long getCurrentPosition() {
@ -290,7 +290,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
/** /**
* Determine if playback file is streaming or local. * Determine if playback file is streaming or local.
* It is streaming if file name starts with "http://" * It is streaming if file name starts with "http://"
* *
* @param file The file name * @param file The file name
* @return T=streaming, F=local * @return T=streaming, F=local
*/ */
@ -305,7 +305,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
/** /**
* Get the duration of the audio file. * Get the duration of the audio file.
* *
* @param file The name of the audio file. * @param file The name of the audio file.
* @return The duration in msec. * @return The duration in msec.
* -1=can't be determined * -1=can't be determined
@ -335,9 +335,9 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
} }
/** /**
* Callback to be invoked when the media source is ready for playback. * Callback to be invoked when the media source is ready for playback.
* *
* @param player The MediaPlayer that is ready for playback * @param player The MediaPlayer that is ready for playback
*/ */
public void onPrepared(MediaPlayer player) { public void onPrepared(MediaPlayer player) {
// Listen for playback completion // Listen for playback completion
@ -363,7 +363,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
/** /**
* By default Android returns the length of audio in mills but we want seconds * By default Android returns the length of audio in mills but we want seconds
* *
* @return length of clip in seconds * @return length of clip in seconds
*/ */
private float getDurationInSeconds() { private float getDurationInSeconds() {
@ -373,7 +373,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
/** /**
* Callback to be invoked when there has been an error during an asynchronous operation * Callback to be invoked when there has been an error during an asynchronous operation
* (other errors will throw exceptions at method call time). * (other errors will throw exceptions at method call time).
* *
* @param player the MediaPlayer the error pertains to * @param player the MediaPlayer the error pertains to
* @param arg1 the type of error that has occurred: (MEDIA_ERROR_UNKNOWN, MEDIA_ERROR_SERVER_DIED) * @param arg1 the type of error that has occurred: (MEDIA_ERROR_UNKNOWN, MEDIA_ERROR_SERVER_DIED)
* @param arg2 an extra code, specific to the error. * @param arg2 an extra code, specific to the error.
@ -392,7 +392,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
/** /**
* Set the state and send it to JavaScript. * Set the state and send it to JavaScript.
* *
* @param state * @param state
*/ */
private void setState(STATE state) { private void setState(STATE state) {
@ -404,7 +404,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
/** /**
* Set the mode and send it to JavaScript. * Set the mode and send it to JavaScript.
* *
* @param state * @param state
*/ */
private void setMode(MODE mode) { private void setMode(MODE mode) {
@ -414,10 +414,10 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
} }
this.mode = mode; this.mode = mode;
} }
/** /**
* Get the audio state. * Get the audio state.
* *
* @return int * @return int
*/ */
public int getState() { public int getState() {
@ -432,7 +432,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
public void setVolume(float volume) { public void setVolume(float volume) {
this.player.setVolume(volume, volume); this.player.setVolume(volume, volume);
} }
/** /**
* attempts to put the player in play mode * attempts to put the player in play mode
* @return true if in playmode, false otherwise * @return true if in playmode, false otherwise
@ -451,7 +451,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
} }
return true; return true;
} }
/** /**
* attempts to initialize the media player for playback * attempts to initialize the media player for playback
* @param file the file to play * @param file the file to play
@ -472,7 +472,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
return false; return false;
case MEDIA_LOADING: case MEDIA_LOADING:
//cordova js is not aware of MEDIA_LOADING, so we send MEDIA_STARTING insntead //cordova js is not aware of MEDIA_LOADING, so we send MEDIA_STARTING insntead
Log.d(LOG_TAG, "AudioPlayer Loading: startPlaying() called during media preparation: " + STATE.MEDIA_STARTING.ordinal()); Log.d(LOG_TAG, "AudioPlayer Loading: startPlaying() called during media preparation: " + STATE.MEDIA_STARTING.ordinal());
this.prepareOnly = false; this.prepareOnly = false;
return false; return false;
case MEDIA_STARTING: case MEDIA_STARTING:
@ -505,13 +505,13 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
} }
return false; return false;
} }
/** /**
* load audio file * load audio file
* @throws IOException * @throws IOException
* @throws IllegalStateException * @throws IllegalStateException
* @throws SecurityException * @throws SecurityException
* @throws IllegalArgumentException * @throws IllegalArgumentException
*/ */
private void loadAudioFile(String file) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException { private void loadAudioFile(String file) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {
if (this.isStreaming(file)) { if (this.isStreaming(file)) {
@ -520,7 +520,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
//if it's a streaming file, play mode is implied //if it's a streaming file, play mode is implied
this.setMode(MODE.PLAY); this.setMode(MODE.PLAY);
this.setState(STATE.MEDIA_STARTING); this.setState(STATE.MEDIA_STARTING);
this.player.setOnPreparedListener(this); this.player.setOnPreparedListener(this);
this.player.prepareAsync(); this.player.prepareAsync();
} }
else { else {
@ -534,13 +534,13 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
if (fp.exists()) { if (fp.exists()) {
FileInputStream fileInputStream = new FileInputStream(file); FileInputStream fileInputStream = new FileInputStream(file);
this.player.setDataSource(fileInputStream.getFD()); this.player.setDataSource(fileInputStream.getFD());
} }
else { else {
this.player.setDataSource("/sdcard/" + file); this.player.setDataSource("/sdcard/" + file);
} }
} }
this.setState(STATE.MEDIA_STARTING); this.setState(STATE.MEDIA_STARTING);
this.player.setOnPreparedListener(this); this.player.setOnPreparedListener(this);
this.player.prepare(); this.player.prepare();
// Get duration // Get duration