in NearbyConnectionsWalkieTalkie/app/src/main/java/com/google/location/nearby/apps/walkietalkie/AudioRecorder.java [47:103]
public void start() {
if (isRecording()) {
Log.w(TAG, "Already running");
return;
}
mAlive = true;
mThread =
new Thread() {
@Override
public void run() {
setThreadPriority(THREAD_PRIORITY_AUDIO);
Buffer buffer = new Buffer();
AudioRecord record =
new AudioRecord(
MediaRecorder.AudioSource.DEFAULT,
buffer.sampleRate,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
buffer.size);
if (record.getState() != AudioRecord.STATE_INITIALIZED) {
Log.w(TAG, "Failed to start recording");
mAlive = false;
return;
}
record.startRecording();
// While we're running, we'll read the bytes from the AudioRecord and write them
// to our output stream.
try {
while (isRecording()) {
int len = record.read(buffer.data, 0, buffer.size);
if (len >= 0 && len <= buffer.size) {
mOutputStream.write(buffer.data, 0, len);
mOutputStream.flush();
} else {
Log.w(TAG, "Unexpected length returned: " + len);
}
}
} catch (IOException e) {
Log.e(TAG, "Exception with recording stream", e);
} finally {
stopInternal();
try {
record.stop();
} catch (IllegalStateException e) {
Log.e(TAG, "Failed to stop AudioRecord", e);
}
record.release();
}
}
};
mThread.start();
}