There is no direct method to mute on AudioRecorder. We need to add some trick for mute the recording.
What I can do is, I download silence wav file and convert it to byte and add on the byte array.
When user click on Mute Button, isMuteClick = true and when unmute it will be false.
while (isStreaming)
{
if(!isMuteClick){
// read() is a blocking call // can set blocking see docs
int bytesRead = recorder.read(readBuffer, 0,bytesReadTotal);
bytesReadTotal += bytesRead; // above ...chunk - bytesReadTotal);
mainBuffer.write(readBuffer, 0, bytesRead);
}else{
int bytesRead = recorder.read(WavToByteArray(R.raw.silence), 0, chunk - bytesReadTotal); // 505 // 4410 //chunk - bytesReadTotal
bytesReadTotal += bytesRead; // above ...chunk - bytesReadTotal);
mainBuffer.write(WavToByteArray(R.raw.silence), 0, bytesRead);
}
} /
And here is code for converting silence wav file to byte array
private byte[] WavToByteArray(int resourceId) {
byte[] filteredByteArray = new byte[1024];
try {
InputStream inputStream = this.getResources().openRawResource(resourceId);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] soundBytes = new byte[1024];
int i = Integer.MAX_VALUE;
while ((i = inputStream.read(soundBytes, 0, soundBytes.length)) > 0) {
outputStream.write(soundBytes, 0, i);
}
inputStream.close();
outputStream.close();
// remove .wav header
byte[] audioBytes = outputStream.toByteArray();
filteredByteArray = Arrays.copyOfRange(audioBytes, 44, audioBytes.length);
} catch (Exception e) {
e.printStackTrace();
}
return filteredByteArray;
}