android sdk realtime audio record <p>I use this code to record and play back recorded audio in real time using the <a href="http://developer.android.com/reference/android/media/AudioTrack.html" rel="nofollow">AudioTrack</a> and <a href="http://developer.android.com/reference/android/media/AudioRecord.html" rel="nofollow">AudioRecord</a></p> <pre><code>package com.example.audiotrack; import android.app.Activity; 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; public class MainActivity extends Activity { private int freq = 8000; private AudioRecord audioRecord = null; private Thread Rthread = null; private AudioManager audioManager = null; private AudioTrack audioTrack = null; byte[] buffer = new byte[freq]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO); final int bufferSize = AudioRecord.getMinBufferSize(freq, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, freq, AudioFormat.CHANNEL_CONFIGURATION_MONO, MediaRecorder.AudioEncoder.AMR_NB, bufferSize); audioTrack = new AudioTrack(AudioManager.ROUTE_HEADSET, freq, AudioFormat.CHANNEL_CONFIGURATION_MONO, MediaRecorder.AudioEncoder.AMR_NB, bufferSize, AudioTrack.MODE_STREAM); audioTrack.setPlaybackRate(freq); final byte[] buffer = new byte[bufferSize]; audioRecord.startRecording(); Log.i("info", "Audio Recording started"); audioTrack.play(); Log.i("info", "Audio Playing started"); Rthread = new Thread(new Runnable() { public void run() { while (true) { try { audioRecord.read(buffer, 0, bufferSize); audioTrack.write(buffer, 0, buffer.length); } catch (Throwable t) { Log.e("Error", "Read write failed"); t.printStackTrace(); } } } }); Rthread.start(); } } </code></pre> <p>My problem : </p> <p>1.the quality of audio is bad</p> <p>2.when I try different frequencies the app crashes</p>