title: Android - How to add my own Audio codec to AudioRecord?
body: <p>I currently have a Loop back program for testing Audio on Android devices.</p>
  
  <p>It uses AudioRecord and AudioTrack to record PCM audio from the Mic and play PCM audio out the earpiece.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public class Record extends Thread
    {
  
            static final int bufferSize = 200000;
            final short[] buffer = new short[bufferSize];
            short[] readBuffer = new short[bufferSize];
  
            public void run() {  
              isRecording = true;
              android.os.Process.setThreadPriority
              (android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
  
              int buffersize = AudioRecord.getMinBufferSize(11025,
              AudioFormat.CHANNEL_CONFIGURATION_MONO,
              AudioFormat.ENCODING_PCM_16BIT);
  
                             arec = new AudioRecord(MediaRecorder.AudioSource.MIC,
                                             11025,
                                             AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                             AudioFormat.ENCODING_PCM_16BIT,
                                             buffersize);
  
                             atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                                             11025,
                                             AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                             AudioFormat.ENCODING_PCM_16BIT,
                                             buffersize,
                                             AudioTrack.MODE_STREAM);
  
  
                             atrack.setPlaybackRate(11025);
  
                             byte[] buffer = new byte[buffersize];
                             arec.startRecording();
                             atrack.play();
  
                             while(isRecording) {
  
                                     arec.read(buffer, 0, buffersize);
                                     atrack.write(buffer, 0, buffer.length);
                             }  
            }
    }
  </code></pre>
  
  <p>So as you can see in the creation of the AudioTrack and AudioRecord the Encoding is supplied via the AudioFormat but this only allows 16 bit or 8 bit PCM.</p>
  
  <p>I have my own G711 Codec implementation now and I want to be able to encode the audio from the Mic and decode it going into the EarPiece, So I have <strong>encode(short lin[], int offset, byte enc[], int frames)</strong> and <strong>decode(byte enc[], short lin[], int frames)</strong> methods but I'm unsure as to how to use them to encode and the decode the audio from the AudioRecord and AudioTrack.</p>
  
  <p>Can anyone help me or point me in the right direction?</p>