androidaudiorecord

Recording .Wav with Android AudioRecorder


I have read a lot of pages about Android's AudioRecorder. You can see a list of them below the question.

I'm trying to record audio with AudioRecorder, but it's not working well.

public class MainActivity extends Activity {

AudioRecord ar = null;
int buffsize = 0;

int blockSize = 256;
boolean isRecording = false;
private Thread recordingThread = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}

public void baslat(View v)
{
            // when click to START 
    buffsize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
    ar = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, buffsize);

    ar.startRecording();

    isRecording = true;
    recordingThread = new Thread(new Runnable() {
        public void run() {
            writeAudioDataToFile();
        }
    }, "AudioRecorder Thread");
    recordingThread.start();
}
public void durdur(View v)
{
            // When click to STOP
    ar.stop();
    isRecording = false;
}

private void writeAudioDataToFile() {
    // Write the output audio in byte

    String filePath = "/sdcard/voice8K16bitmono.wav";
    short sData[] = new short[buffsize/2];

    FileOutputStream os = null;
    try {
        os = new FileOutputStream(filePath);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    while (isRecording) {
        // gets the voice output from microphone to byte format

        ar.read(sData, 0, buffsize/2);
        Log.d("eray","Short wirting to file" + sData.toString());
        try {
            // // writes the data to file from buffer
            // // stores the voice buffer
            byte bData[] = short2byte(sData);
            os.write(bData, 0, buffsize);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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;

}

It's creating a .wav file but, when I try to listen to it, it's not opening. I'm getting a "file not supported" error. I've tried to play the file with quite a few media player applications.

NOTE : I have to use AudioRecorder instead of MediaRecorder because my app will be doing another process while recording (displaying an equalizer) .

Here is the list of pages that I've read about this subject:

  1. http://developer.android.com/reference/android/media/AudioRecord.html#read(short[],%20int,%20int)
  2. Android AudioRecord example
  3. http://audiorecordandroid.blogspot.in
  4. AudioRecord object not initializing
  5. Recording a wav file from the mic in Android - problems
  6. http://i-liger.com/article/android-wav-audio-recording
  7. Creating a WAV file from raw PCM data using the Android SDK
  8. Capturing Sound for Analysis and Visualizing Frequencies in Android

There are a lot of different ways to go about this. I've tried lots of them but nothing works for me. I've been working on this problem for about 6 hours now so I would appreciate a definitive answer, ideally some sample code.


Solution

  • PCMAudioHelper solved my problem. I'll modify this answer and explain it but firstly i have to do some tests over this class.