Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AudioRecord example [closed]

I am designing an Android app and I need to implement an AudioRecord class to record the user's sound. After some research (that didn't provide enough information) and few failed attempts, I was wondering if anyone could help me by posting an example (code) on how to capture high quality sound using AudioRecord. I would really appreciate it. Thank you

like image 229
Ziki Kongawi Avatar asked Dec 14 '11 02:12

Ziki Kongawi


People also ask

Do I have a voice recorder on this phone?

Many phones have a built-in voice recording app, and Android is no exception. Every Android phone will have a voice recording app already installed on it called Recorder. The appearance and quality of the app may vary depending on the model of the phone and the version of Android you have, but it is easy to use.

How do I use voice recorder on Android?

Open the App Drawer by swiping up from the bottom of your screen. 2. If you don't immediately see the Voice Recorder app, you may need to open a folder that will likely have the phone's name as its label (Samsung, e.g.). Do so, then tap the Voice Recorder app.

How do I record faster on Android?

Swipe down from the top of the phone screen twice to open the Quick Settings menu. You may need to swipe left to locate the Screen Recorder app. Tap on it and then tap Start now. The Screen Recorder menu will come up on the screen with the buttons for Record, Settings, and Close/Stop.


2 Answers

Here is an end to end solution I implemented for streaming Android microphone audio to a server for playback: Android AudioRecord to Server over UDP Playback Issues

like image 36
Joshua W Avatar answered Sep 19 '22 19:09

Joshua W


Here I am posting you the some code example which record good quality of sound using AudioRecord API.

Note: If you use in emulator the sound quality will not much good because we are using sample rate 8k which only supports in emulator. In device use sample rate to 44.1k for better quality.

public class Audio_Record extends Activity {     private static final int RECORDER_SAMPLERATE = 8000;     private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;     private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;     private AudioRecord recorder = null;     private Thread recordingThread = null;     private boolean isRecording = false;      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);          setButtonHandlers();         enableButtons(false);          int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,                 RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);      }      private void setButtonHandlers() {         ((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);         ((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);     }      private void enableButton(int id, boolean isEnable) {         ((Button) findViewById(id)).setEnabled(isEnable);     }      private void enableButtons(boolean isRecording) {         enableButton(R.id.btnStart, !isRecording);         enableButton(R.id.btnStop, isRecording);     }      int BufferElements2Rec = 1024; // want to play 2048 (2K) since 2 bytes we use only 1024     int BytesPerElement = 2; // 2 bytes in 16bit format      private void startRecording() {          recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,                 RECORDER_SAMPLERATE, RECORDER_CHANNELS,                 RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement);          recorder.startRecording();         isRecording = true;         recordingThread = new Thread(new Runnable() {             public void run() {                 writeAudioDataToFile();             }         }, "AudioRecorder Thread");         recordingThread.start();     }          //convert short to byte     private byte[] short2byte(short[] sData) {         int shortArrsize = sData.length;         byte[] bytes = new byte[shortArrsize * 2];         for (int i = 0; i < shortArrsize; i++) {             bytes[i * 2] = (byte) (sData[i] & 0x00FF);             bytes[(i * 2) + 1] = (byte) (sData[i] >> 8);             sData[i] = 0;         }         return bytes;      }      private void writeAudioDataToFile() {         // Write the output audio in byte          String filePath = "/sdcard/voice8K16bitmono.pcm";         short sData[] = new short[BufferElements2Rec];          FileOutputStream os = null;         try {             os = new FileOutputStream(filePath);         } catch (FileNotFoundException e) {             e.printStackTrace();         }          while (isRecording) {             // gets the voice output from microphone to byte format              recorder.read(sData, 0, BufferElements2Rec);             System.out.println("Short writing to file" + sData.toString());             try {                 // // writes the data to file from buffer                 // // stores the voice buffer                 byte bData[] = short2byte(sData);                 os.write(bData, 0, BufferElements2Rec * BytesPerElement);             } catch (IOException e) {                 e.printStackTrace();             }         }         try {             os.close();         } catch (IOException e) {             e.printStackTrace();         }     }      private void stopRecording() {         // stops the recording activity         if (null != recorder) {             isRecording = false;             recorder.stop();             recorder.release();             recorder = null;             recordingThread = null;         }     }      private View.OnClickListener btnClick = new View.OnClickListener() {         public void onClick(View v) {             switch (v.getId()) {             case R.id.btnStart: {                 enableButtons(true);                 startRecording();                 break;             }             case R.id.btnStop: {                 enableButtons(false);                 stopRecording();                 break;             }             }         }     };      @Override     public boolean onKeyDown(int keyCode, KeyEvent event) {         if (keyCode == KeyEvent.KEYCODE_BACK) {             finish();         }         return super.onKeyDown(keyCode, event);     } } 

For more detail try this AUDIORECORD BLOG.

Happy Coding !!

like image 174
Rahul Baradia Avatar answered Sep 20 '22 19:09

Rahul Baradia