Better error handling and debug output.
Signed-off-by: Marti Bolivar <mbolivar@leaflabs.com>
diff --git a/src/com/android/speechrecorder/SpeechRecorderActivity.java b/src/com/android/speechrecorder/SpeechRecorderActivity.java
index b2dcf3f..7b22141 100644
--- a/src/com/android/speechrecorder/SpeechRecorderActivity.java
+++ b/src/com/android/speechrecorder/SpeechRecorderActivity.java
@@ -85,7 +85,7 @@
mCommand.setText("Please click 'Record' to begin");
mRecord.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
- if (false) {
+ if (true) {
Log.d(TAG, "mRecord.OnClickListener.onClick");
}
@@ -150,7 +150,7 @@
Log.d(TAG, "going to record " + mUtterance.toString());
// fix up UI
- mCommand.setText("Say: \"" + utterances[index] + "\"");
+ mCommand.setText("Starting the recording...");
final String status = "item " + (index + 1) + "/" + utterances.length;
// start the microphone
@@ -164,7 +164,9 @@
// mMicrophone = logInputStream(mUtterance.toString(), mMicrophone, mSampleRate);
} catch (IOException e) {
-
+ Log.e(TAG, "Can't open microphone, aborting: " + e);
+ stopRecording(false);
+ return;
}
// post a number of delayed events to update the UI and to stop recording
@@ -196,6 +198,8 @@
rtn = mMicrophone.read(buffer, 0, 512);
if (rtn > 0) mBaos.write(buffer, 0, rtn);
} catch (IOException e) {
+ Log.e(TAG, "error in audio capture thread: " + e);
+ stopRecording(false);
}
}
}
@@ -211,41 +215,58 @@
}
private void stopRecording() {
- Log.d(TAG, "stopRecording");
- mStoppedListening = true;
- try {
- mThread.join();
- } catch (InterruptedException e) {
-
- }
- try {
- OutputStream out = new FileOutputStream(mUtterance.toString());
- try {
- byte[] pcm = mBaos.toByteArray();
- Log.d(TAG, "byteArray length " + pcm.length);
- WaveHeader hdr = new WaveHeader(WaveHeader.FORMAT_PCM,
- (short)1, mSampleRate, (short)16, pcm.length);
- hdr.write(out);
- out.write(pcm);
- } finally {
- out.close();
- mMicrophone.close();
- mBaos.close();
- }
- } catch (IOException e) {
-
-
- } finally {
- }
-
- // stop the recording
- mRecord.setEnabled(true);
-
- mRedo.setEnabled(true);
-
- mCommand.setText("Got it!");
+ stopRecording(true);
}
+ private void stopRecording(boolean finished) {
+ Log.i(TAG, "stopRecording");
+ mStoppedListening = true;
+ if (finished) {
+ try {
+ mThread.join();
+ } catch (InterruptedException e) {
+ Log.e(TAG, "error while stopping audio recording thread: " + e);
+ }
+ try {
+ OutputStream out = new FileOutputStream(mUtterance.toString());
+ try {
+ byte[] pcm = mBaos.toByteArray();
+ Log.d(TAG, "pcm length " + pcm.length);
+ if (isAllZero(pcm)) {
+ Log.w(TAG, "pcm array is all zero");
+ } else {
+ Log.i(TAG, "got some PCM data");
+ }
+ WaveHeader hdr = new WaveHeader(WaveHeader.FORMAT_PCM,
+ (short)1, mSampleRate, (short)16, pcm.length);
+ hdr.write(out);
+ out.write(pcm);
+ } finally {
+ out.close();
+ mMicrophone.close();
+ mBaos.close();
+ }
+ } catch (IOException e) {
+ Log.e(TAG, "error while writing utterance: " + e);
+ }
+ }
+ // Allow a new recording
+ mRecord.setEnabled(true);
+ mRedo.setEnabled(true);
+ if (finished) {
+ mCommand.setText("Got it!");
+ } else {
+ mCommand.setText("Didn't get that :(");
+ }
+ }
+
+ private boolean isAllZero(byte[] arr) {
+ for (byte b: arr) {
+ if (b != 0)
+ return false;
+ }
+ return true;
+ }
private final static String[] mCallUtterances = new String[] {
"Call Adam Varro",