Avoids crash when StartRecording conflicts with existing recording application

BUG=b/21066709
R=hbos@webrtc.org, magjed@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/56379004

Cr-Commit-Position: refs/heads/master@{#9235}
diff --git a/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java b/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java
index 12e8dd0..02ebe5b 100644
--- a/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java
+++ b/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java
@@ -69,13 +69,6 @@
     public void run() {
       Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO);
       Logd("AudioRecordThread" + WebRtcAudioUtils.getThreadInfo());
-
-      try {
-        audioRecord.startRecording();
-      } catch (IllegalStateException e) {
-          Loge("AudioRecord.startRecording failed: " + e.getMessage());
-        return;
-      }
       assertTrue(audioRecord.getRecordingState()
           == AudioRecord.RECORDSTATE_RECORDING);
 
@@ -227,6 +220,16 @@
     Logd("StartRecording");
     assertTrue(audioRecord != null);
     assertTrue(audioThread == null);
+    try {
+      audioRecord.startRecording();
+    } catch (IllegalStateException e) {
+      Loge("AudioRecord.startRecording failed: " + e.getMessage());
+      return false;
+    }
+    if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
+      Loge("AudioRecord.startRecording failed");
+      return false;
+    }
     audioThread = new AudioRecordThread("AudioRecordJavaThread");
     audioThread.start();
     return true;