Simple echo application for how to use android AudioTrack and AudioRecord classes
Download Source
Step 1
Create MainActivity class for audio record and play
Step 2
Add Button for Play/Pause and Call mode/ Speaker mode
Step 3
Add audio related permission to Android.xml
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>
No comments:
Post a Comment