Commit 8cbe40cf authored by Evan W. Patton's avatar Evan W. Patton

Fix SoundRecorder crashing on Start method call

Reported by the forum posting [1], the SoundRecorder crashes when the
Start method is called. After some debugging, this is due to
unexpected values being reported via the onInfo method being called
with an unexpected, non-documented value of `what`. The code always
called controller.recorder.stop(), but since the MediaRecorder hadn't
transitioned to the recording state yet, stop() throws an uncaught
IllegalStateException, which kills the program.

This commit makes the onInfo method more robust by reporting known
`what` values via dispatchErrorOccurredEvent and ignoring any values
not declared by AOSP. It also adds a call to StoppedRecording()
otherwise the app may not have been aware of the failure state.

[1] https://groups.google.com/d/msg/mitappinventortest/qMPzn5s82UY/LJu8znfrAwAJ

Change-Id: Ifd12fcf016df07b500b958bd4900781bf6ff6c47
parent 160f16c0
......@@ -202,8 +202,35 @@ public final class SoundRecorder extends AndroidNonvisibleComponent
Log.w(TAG, "onInfo called with wrong recorder. Ignoring.");
return;
}
Log.i(TAG, "Recoverable condition while recording. Will attempt to stop normally.");
controller.recorder.stop();
switch (what) {
case MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED:
form.dispatchErrorOccurredEvent(this, "recording",
ErrorMessages.ERROR_SOUND_RECORDER_MAX_DURATION_REACHED);
break;
case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED:
form.dispatchErrorOccurredEvent(this, "recording",
ErrorMessages.ERROR_SOUND_RECORDER_MAX_FILESIZE_REACHED);
break;
case MediaRecorder.MEDIA_RECORDER_INFO_UNKNOWN:
form.dispatchErrorOccurredEvent(this, "recording", ErrorMessages.ERROR_SOUND_RECORDER);
break;
default:
// value of `what` is not valid, probably device-specific debugging. escape early to prevent
// stoppage until we see an Android-defined error. See also:
// http://stackoverflow.com/questions/25785420/mediarecorder-oninfolistener-giving-an-895
return;
}
try {
Log.i(TAG, "Recoverable condition while recording. Will attempt to stop normally.");
controller.recorder.stop();
} catch(IllegalStateException e) {
Log.i(TAG, "SoundRecorder was not in a recording state.", e);
form.dispatchErrorOccurredEventDialog(this, "Stop",
ErrorMessages.ERROR_SOUND_RECORDER_ILLEGAL_STOP);
} finally {
controller = null;
StoppedRecording();
}
}
/**
......
......@@ -100,6 +100,9 @@ public final class ErrorMessages {
// SoundRecorder errors
public static final int ERROR_SOUND_RECORDER = 801;
public static final int ERROR_SOUND_RECORDER_CANNOT_CREATE = 802;
public static final int ERROR_SOUND_RECORDER_ILLEGAL_STOP = 803;
public static final int ERROR_SOUND_RECORDER_MAX_DURATION_REACHED = 804;
public static final int ERROR_SOUND_RECORDER_MAX_FILESIZE_REACHED = 805;
// Form errors
public static final int ERROR_INVALID_SCREEN_ORIENTATION = 901;
public static final int ERROR_SCREEN_NOT_FOUND = 902;
......@@ -385,6 +388,9 @@ public final class ErrorMessages {
// SoundRecorder errors
errorMessages.put(ERROR_SOUND_RECORDER, "An unexpected error occurred while recording sound.");
errorMessages.put(ERROR_SOUND_RECORDER_CANNOT_CREATE, "Cannot start recording: %s");
errorMessages.put(ERROR_SOUND_RECORDER_ILLEGAL_STOP, "Stop() called when not recording.");
errorMessages.put(ERROR_SOUND_RECORDER_MAX_DURATION_REACHED, "Maximum sound recording duration was reached.");
errorMessages.put(ERROR_SOUND_RECORDER_MAX_FILESIZE_REACHED, "Maximum sound recording size was reached.");
// Form errors
errorMessages.put(ERROR_INVALID_SCREEN_ORIENTATION,
"The specified screen orientation is not valid: %s");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment