CB-1267: Reuse Media object for recording

This commit is contained in:
macdonst 2012-08-20 16:23:19 -04:00
parent 8d0e80620a
commit afe504dbbf
2 changed files with 18 additions and 20 deletions

View File

@ -20,6 +20,7 @@ package org.apache.cordova;
import android.content.Context; import android.content.Context;
import android.media.AudioManager; import android.media.AudioManager;
import java.util.ArrayList; import java.util.ArrayList;
import org.apache.cordova.api.Plugin; import org.apache.cordova.api.Plugin;
@ -214,7 +215,6 @@ public class AudioHandler extends Plugin {
AudioPlayer audio = this.players.get(id); AudioPlayer audio = this.players.get(id);
if (audio != null) { if (audio != null) {
audio.stopRecording(); audio.stopRecording();
this.players.remove(id);
} }
} }

View File

@ -96,6 +96,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
this.handler = handler; this.handler = handler;
this.id = id; this.id = id;
this.audioFile = file; this.audioFile = file;
this.recorder = new MediaRecorder();
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
this.tempFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmprecording.3gp"; this.tempFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmprecording.3gp";
@ -137,26 +138,22 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});"); this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
break; break;
case NONE: case NONE:
// Make sure we're not already recording this.audioFile = file;
if (this.recorder == null) { this.recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
this.audioFile = file; this.recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // THREE_GPP);
this.recorder = new MediaRecorder(); this.recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); //AMR_NB);
this.recorder.setAudioSource(MediaRecorder.AudioSource.MIC); this.recorder.setOutputFile(this.tempFile);
this.recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // THREE_GPP); try {
this.recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); //AMR_NB); this.recorder.prepare();
this.recorder.setOutputFile(this.tempFile); this.recorder.start();
try { this.setState(STATE.MEDIA_RUNNING);
this.recorder.prepare(); return;
this.recorder.start(); } catch (IllegalStateException e) {
this.setState(STATE.MEDIA_RUNNING); e.printStackTrace();
return; } catch (IOException e) {
} catch (IllegalStateException e) { e.printStackTrace();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
} }
this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});");
break; break;
case RECORD: case RECORD:
Log.d(LOG_TAG, "AudioPlayer Error: Already recording."); Log.d(LOG_TAG, "AudioPlayer Error: Already recording.");
@ -191,6 +188,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
this.recorder.stop(); this.recorder.stop();
this.setState(STATE.MEDIA_STOPPED); this.setState(STATE.MEDIA_STOPPED);
} }
this.recorder.reset();
this.moveFile(this.audioFile); this.moveFile(this.audioFile);
} }
catch (Exception e) { catch (Exception e) {