android - save sound recording in a buffer and play it later
<p>I'm trying to make a recording without save sound file, I use AudioRecord to save the recording sound in a buffer and AudioTrack for playback the sound later when "Play" Button tapped</p>

<p>this my code source of application</p>

<pre><code>package com.stackoverflow.sofproject;

import java.io.IOException;

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.util.Log;
import android.view.View;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {

    boolean recorder = true;
    AudioRecord ar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final int minSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
        ar = new AudioRecord(MediaRecorder.AudioSource.MIC,
                8000,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,minSize);


        final short[] buffer = new short[minSize];

        ar.startRecording();        
        Log.e("SoundRecordingActivity", "Start record !");

        Thread Rthread = new Thread(new Runnable() {
            public void run() {
                while(recorder)
                {
                    ar.read(buffer, 0, minSize);
                }
            }
        });
        Rthread.start();
    }

    public void stopRecordButtonAndPlayBack(){
        ar.stop();
        recorder = false;
        Log.e("SoundRecordingActivity", "Stop record !");
        // play sound
        int minSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
        ar = new AudioRecord(MediaRecorder.AudioSource.MIC,
                8000,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,minSize);

        AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                8000,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                 AudioFormat.ENCODING_PCM_16BIT,
                minSize,
                AudioTrack.MODE_STREAM);

        audioTrack.play();

        while (true) {
            audioTrack.write(buffer, 0, minSize);
        }           
    }
}
</code></pre>

<p>the recording work fine</p>

<p>when I tape the play button (stopRecordButtonAndPlayBack) the app force close</p>

<p>the problem not in the link between button and stopRecordButtonAndPlayBack
the problem in how to play the buffer as a sound</p>

<p>Sorry for my English</p>
