In this video, it shows how one can create a simple Android App to record and play the sound simultaneously.
The concept taken in this video is very simple. It first records the sound using the AudioRecord and plays the same sound using the AudioTrack in a while loop.
For transferring the data packets, it creates a local variable shortAudioData of type short. This variable is minimum buffer size for the ENCODING_PCM_16BIT encoding.
This kind of App can be used for monitoring your kids (baby monitor) at home or even spying. Say for example the kids are in one room and the adult can be another room with the speaker which continuously receive the sound of the kids room through this App.
As told in the video, this App has been hosted on the Play Store and can be referred using the below link:
https://play.google.com/store/apps/de...
I hope you liked this video. For any questions, suggestions or appreciations please contact us at: https://programmerworld.co/contact/ or email us at: [email protected]
The complete source code of this video is shared at the below page:
https://programmerworld.co/android/re...
However, we are copying the Java code below as well for your reference:
package com.programmerworld.babymonitor;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textViewStatus;
private EditText editTextGainFactor;
private AudioRecord audioRecord;
private AudioTrack audioTrack;
private int intBufferSize;
private short[] shortAudioData;
private int intGain;
private boolean isActive = false;
private Thread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, PackageManager.PERMISSION_GRANTED);
textViewStatus = findViewById(R.id.textViewStatus);
editTextGainFactor = findViewById(R.id.editTextGainFactor);
thread = new Thread(new Runnable() {
@Override
public void run() {
threadLoop();
}
});
}
public void buttonStart(View view){
isActive = true;
intGain = Integer.parseInt(editTextGainFactor.getText().toString());
textViewStatus.setText("Active");
thread.start();
}
public void buttonStop(View view){
isActive = false;
audioTrack.stop();
audioRecord.stop();
textViewStatus.setText("Stopped");
}
private void threadLoop(){
int intRecordSampleRate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_MUSIC);
intBufferSize = AudioRecord.getMinBufferSize(intRecordSampleRate, AudioFormat.CHANNEL_IN_MONO
, AudioFormat.ENCODING_PCM_16BIT);
shortAudioData = new short[intBufferSize];
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC
, intRecordSampleRate
, AudioFormat.CHANNEL_IN_STEREO
, AudioFormat.ENCODING_PCM_16BIT
, intBufferSize);
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC
, intRecordSampleRate
, AudioFormat.CHANNEL_IN_STEREO
, AudioFormat.ENCODING_PCM_16BIT
, intBufferSize
, AudioTrack.MODE_STREAM);
audioTrack.setPlaybackRate(intRecordSampleRate);
audioRecord.startRecording();
audioTrack.play();
while (isActive){
audioRecord.read(shortAudioData, 0, shortAudioData.length);
for (int i = 0; i LESS_THAN shortAudioData.length; i++){
shortAudioData[i] = (short) Math.min (shortAudioData[i] * intGain, Short.MAX_VALUE);
}
audioTrack.write(shortAudioData, 0, shortAudioData.length);
}
}
}