Android Open Source - voicr Record Audio Fragment






From Project

Back to project page voicr.

License

The source code is released under:

MIT License

If you think the Android project voicr listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package info.androidhive.slidingmenu;
//from  w w  w.j a  v  a 2s . c  o  m
import android.app.Fragment;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class RecordAudioFragment extends Fragment {

  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 View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.audio_record, container, false);
    return rootView;
  }


  @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    setButtonHandlers();
      enableButtons(false);

      int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
              RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING); 
    
    
    }

  private void setButtonHandlers() {
    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;
              }
              }
          }
      };
  ((Button) getView().findViewById(R.id.btnStart)).setOnClickListener(btnClick);
    ((Button) getView().findViewById(R.id.btnStop)).setOnClickListener(btnClick);
}

private void enableButton(int id, boolean isEnable) {
    ((Button) getView().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 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, 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;
    }
}

}




Java Source Code List

info.androidhive.slidingmenu.CommunityFragment.java
info.androidhive.slidingmenu.FindPeopleFragment.java
info.androidhive.slidingmenu.HomeFragment.java
info.androidhive.slidingmenu.MainActivity.java
info.androidhive.slidingmenu.NotesList.java
info.androidhive.slidingmenu.PagesFragment.java
info.androidhive.slidingmenu.PhotosFragment.java
info.androidhive.slidingmenu.RecordAudioFragment.java
info.androidhive.slidingmenu.RecordAudio.java
info.androidhive.slidingmenu.WhatsHotFragment.java
info.androidhive.slidingmenu.adapter.NavDrawerListAdapter.java
info.androidhive.slidingmenu.model.NavDrawerItem.java