Now uses builder class for AudioRecord objects from SDK 23.

Cleanup CL. Start using new AudioRecord.Builder class for creating
AudioRecord Java instances. Exists from API 23.

BUG=webrtc:7962

Review-Url: https://codereview.webrtc.org/3007673002
Cr-Original-Commit-Position: refs/heads/master@{#19571}
Cr-Mirrored-From: https://chromium.googlesource.com/external/webrtc
Cr-Mirrored-Commit: d4495312dc7f6b5d5fc1c163026c52d56e6230f9
diff --git a/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java b/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java
index 66c1483..729ff37 100644
--- a/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java
+++ b/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java
@@ -215,8 +215,16 @@
     int bufferSizeInBytes = Math.max(BUFFER_SIZE_FACTOR * minBufferSize, byteBuffer.capacity());
     Logging.d(TAG, "bufferSizeInBytes: " + bufferSizeInBytes);
     try {
-      audioRecord = new AudioRecord(AudioSource.VOICE_COMMUNICATION, sampleRate, channelConfig,
-          AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes);
+      if (WebRtcAudioUtils.runningOnMarshmallowOrHigher()) {
+        // Use AudioRecord.Builder to create the AudioRecord instance if we are on API level 23 or
+        // higher.
+        audioRecord = createAudioRecordOnMarshmallowOrHigher(
+            sampleRate, channelConfig, bufferSizeInBytes);
+      } else {
+        // Use default constructor for API levels below 23.
+        audioRecord = new AudioRecord(AudioSource.VOICE_COMMUNICATION, sampleRate,
+            channelConfig, AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes);
+      }
     } catch (IllegalArgumentException e) {
       reportWebRtcAudioRecordInitError("AudioRecord ctor error: " + e.getMessage());
       releaseAudioResources();
@@ -305,6 +313,22 @@
     }
   }
 
+  // Creates an AudioRecord instance using AudioRecord.Builder which was added in API level 23.
+  @TargetApi(23)
+  private AudioRecord createAudioRecordOnMarshmallowOrHigher(
+    int sampleRateInHz, int channelConfig, int bufferSizeInBytes) {
+    Logging.d(TAG, "createAudioRecordOnMarshmallowOrHigher");
+    return new AudioRecord.Builder()
+        .setAudioSource(AudioSource.VOICE_COMMUNICATION)
+        .setAudioFormat(new AudioFormat.Builder()
+            .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
+            .setSampleRate(sampleRateInHz)
+            .setChannelMask(channelConfig)
+            .build())
+        .setBufferSizeInBytes(bufferSizeInBytes)
+        .build();
+  }
+
   // Helper method which throws an exception  when an assertion has failed.
   private static void assertTrue(boolean condition) {
     if (!condition) {