Android Audio Demo - AudioTrack , AudioRecord - (Echo Sample)

Simple echo application for how to use android AudioTrack and AudioRecord classes
Download Source
Step 1
    Create MainActivity class for audio record and play

package com.javaorigin.audio;

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.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
 AudioManager am = null;
 AudioRecord record =null;
 AudioTrack track =null;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  setVolumeControlStream(AudioManager.MODE_IN_COMMUNICATION);
  init();
  (new Thread() {
   @Override
   public void run() {
    recordAndPlay();
   }
  }).start();
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 private void init() {
  int min = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
  record = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, 8000, AudioFormat.CHANNEL_IN_MONO,
    AudioFormat.ENCODING_PCM_16BIT, min);

  int maxJitter = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, 8000, AudioFormat.CHANNEL_OUT_MONO,
    AudioFormat.ENCODING_PCM_16BIT, maxJitter, AudioTrack.MODE_STREAM);
 }

 private void recordAndPlay() {
  short[] lin = new short[1024];
  int num = 0;
  am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
  am.setMode(AudioManager.MODE_IN_COMMUNICATION);
  record.startRecording();
  track.play();
  while (true) {
   num = record.read(lin, 0, 1024);
   track.write(lin, 0, num);
  }
 }
 
 boolean isSpeaker = false;

 public void modeChange(View view) {
  Button modeBtn=(Button) findViewById(R.id.modeBtn);
  if (isSpeaker == true) {   
   am.setSpeakerphoneOn(false);
   isSpeaker = false;
   modeBtn.setText("Call Mode");
  } else {   
   am.setSpeakerphoneOn(true);
   isSpeaker = true;
   modeBtn.setText("Speaker Mode");
  }
 }
    
 boolean isPlaying=true;
 public void play(View view){
  Button playBtn=(Button) findViewById(R.id.playBtn);
  if(isPlaying){
   record.stop();
   track.pause();
   isPlaying=false;
   playBtn.setText("Play");
  }else{
   record.startRecording();
   track.play();
   isPlaying=true;
   playBtn.setText("Pause");
  }
 }
 
}
Step 2
        Add Button for Play/Pause and Call mode/ Speaker mode
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >



    <Button
        android:id="@+id/modeBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="24dp"
        android:text="Call Mode" 
        android:onClick="modeChange"/>

    <Button
        android:id="@+id/playBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/modeBtn"
        android:layout_alignBottom="@+id/modeBtn"
        android:layout_marginLeft="30dp"
        android:layout_toRightOf="@+id/modeBtn"
        android:onClick="play"
        android:text="Pause" />

</RelativeLayout>

Step 3
    Add audio related permission to Android.xml
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"></uses-permission>
    <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>

11 comments :

  1. pls upload screen shots of app as well with your source code, thnks for this code ..

    ReplyDelete
  2. Works Great.......

    But I want to give delay in replying the sound ....for that I have done maxJitter*19 . and it worked
    But I have increased the audiotrack frequency i.e.
    track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, 9000, AudioFormat.CHANNEL_OUT_MONO,
    AudioFormat.ENCODING_PCM_16BIT, maxJitter, AudioTrack.MODE_STREAM);

    after giving 9000 frequency it gives me delay for sometimes only and after that it immediately replies to my voice which I really don't want ....

    Plz Help me in this...
    Thanks in advance

    ReplyDelete
  3. short[] lin = new short[1024];
    int num = 0;

    I wonder why lin.length() = 1024??
    how do you have this num?

    ReplyDelete
  4. int min = AudioRecord.getMinBufferSize(8000,
    AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT);
    record = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION,
    8000, AudioFormat.CHANNEL_IN_STEREO,
    AudioFormat.ENCODING_PCM_16BIT, min);

    int maxJitter = AudioTrack.getMinBufferSize(8000,
    AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT);
    track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, 8000,
    AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
    maxJitter, AudioTrack.MODE_STREAM);

    when i change AudioFormat.CHANNEL_OUT_MONO to AudioFormat.CHANNEL_OUT_STEREO, it can't work?!!
    Some one help me plz!!
    thanks

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. How to add resource mp3 file to AudioTrack

    ReplyDelete
  7. hi frnds,

    i want to record my incoming and out going calls in my phone

    can you send me code for this

    Thanks

    ReplyDelete
  8. hi frnds,

    i want to record my incoming and out going calls in my phone

    can you send me code for this

    Thanks

    ReplyDelete