lena1990 -3 Junior Poster in Training

i want to record voice then play it at the same time but when i use the below code i got delay and echo of the voice and some noise i will be grateful for any help

 int bufferSize = AudioRecord.getMinBufferSize(frequency,
                    channelConfiguration, audioEncoding);
            AudioRecord audioRecord = new AudioRecord(
                    MediaRecorder.AudioSource.MIC, frequency,
                    channelConfiguration, audioEncoding, bufferSize);
            short[] buffer = new short[bufferSize];
            AudioTrack audioTrack = new AudioTrack(
                    AudioManager.STREAM_MUSIC, frequency,
                    channelConfiguration, audioEncoding, bufferSize,
              short[] audiobuffer=new short[bufferSize/4];
            int buffersizeno=bufferSize/4;
            int r = 0;
            int k=0;
            while (isRecording) {
                int bufferReadResult = audioRecord.read(buffer, 0,
                for (int i = 0; i < bufferReadResult; i++) {
                    if (k<buffersizeno){
                    else {
                        audioTrack.write(audiobuffer, 0, audiobuffer.length);
                        audiobuffer=new short[bufferSize/4];

                publishProgress(new Integer(r));
         //   dos.close();
        } catch (Throwable t) {
            Log.e("AudioRecord", "Recording Failed");
        return null;

in case you are wondering why i am using another array of size buffersize/4 here is the cause

We’ll use an array of shorts to hold the audio we read from the AudioRecord object. We’ll make the array smaller than the buffer that the AudioRecord object has so that buffer won’t fill up before we read it out. To make sure this array is smaller than the buffer size, we divide by 4. The size of the buffer is in bytes and each short takes up 2 bytes, so dividing by 2 won’t be enough. Dividing by 4 will make it so that this array is half the size of the AudioRecord object’s internal buffer. short[] buffer = new short[bufferSize/4];