2

Here I am trying to do recording and playing a voice at a time, It is working fine for few seconds and force closing showing error at buffer writing to audiotrack(m_track.write(buffer, 0, BUF_SIZE);). Please check the below code, and suggest whether I am initializing audio track object correctly.

    int SAMPLE_RATE = 8000;
    int BUF_SIZE = 1024;
    byte[] buffer = new byte[BUF_SIZE];

function()
{
try
{
            buffersize = AudioRecord
                    .getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
                            AudioFormat.ENCODING_PCM_16BIT);

        } catch (Throwable t) {
            Log.e("Audio", "Buffer size failed");
        }

        if (buffersize <= BUF_SIZE) {
            buffersize = BUF_SIZE;
        }
        try {

            m_record = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, buffersize * 2);

            m_track = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                    SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, buffersize * 2,
                    AudioTrack.MODE_STREAM);

            m_track.setPlaybackRate(SAMPLE_RATE);
        } catch (Throwable t) {
            Log.e("Audio", "Audio Failed");
        }

        m_record.startRecording();
        m_track.play();
        try {
            m_thread = new Runnable() {
                public void run() {
                    while (m_isRun) {

                        m_record.read(buffer, 0, BUF_SIZE);
                        m_track.write(buffer, 0, BUF_SIZE);
                    }
                }
            };

            m_thread.run();
        } catch (Exception e) {
            Log.e("ERROR", "exception: " + e.getLocalizedMessage());
        }

        Log.i("D2Record", "loopback exit");
    }
candy
  • 734
  • 1
  • 11
  • 22
  • How u solved this problem ? I am also getting same problem... plz help http://stackoverflow.com/questions/9413998/live-audio-recording-and-playing-in-android-and-thread-callback-handling – Amit Mar 01 '12 at 13:06

1 Answers1

2

I'm not exactly familiar with the topic but I think you're having problems with buffer sizes. I'm leaving most of the code away and concentrate mainly on buffer handling.

int SAMPLE_RATE = 8000;
int BUF_SIZE = 1024;

int buffersize = AudioRecord
                .getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
                        AudioFormat.ENCODING_PCM_16BIT);
bufferSize = Math.Max(bufferSize, AudioTrack
                .getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
                        AudioFormat.ENCODING_PCM_16BIT);
bufferSize = Math.max(bufferSize, BUF_SIZE);
byte[] buffer = new byte[bufferSize];

m_record = new AudioRecord(MediaRecorder.AudioSource.MIC,
                SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT, bufferSize);
m_track = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
                AudioFormat.ENCODING_PCM_16BIT, bufferSize,
                AudioTrack.MODE_STREAM);

m_track.setPlaybackRate(SAMPLE_RATE);
m_record.startRecording();
m_track.play();
while (m_isRun) {
    // You should check 'read' for errors after read.
    int read = m_record.read(buffer, 0, bufferSize);
    // You should check 'wrote' for errors after write.
    int wrote = m_track.write(buffer, 0, read);
}
harism
  • 6,011
  • 1
  • 36
  • 31