Custom Audio Recorder : Audio « Media « Android






Custom Audio Recorder

   
package app.test;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Test extends Activity implements OnClickListener,
    OnCompletionListener {

  TextView statusTextView;

  Button startRecording, stopRecording, playRecording, finishButton;

  MediaRecorder recorder;
  MediaPlayer player;

  File audioFile;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    statusTextView = (TextView) this.findViewById(R.id.StatusTextView);
    statusTextView.setText("Ready");

    stopRecording = (Button) this.findViewById(R.id.StopRecording);
    startRecording = (Button) this.findViewById(R.id.StartRecording);
    playRecording = (Button) this.findViewById(R.id.PlayRecording);
    finishButton = (Button) this.findViewById(R.id.FinishButton);

    startRecording.setOnClickListener(this);
    stopRecording.setOnClickListener(this);
    playRecording.setOnClickListener(this);
    finishButton.setOnClickListener(this);

    stopRecording.setEnabled(false);
    playRecording.setEnabled(false);
  }

  public void onClick(View v) {
    if (v == finishButton) {
      finish();
    } else if (v == stopRecording) {
      recorder.stop();
      recorder.release();
      player = new MediaPlayer();
      player.setOnCompletionListener(this);
      try {
        player.setDataSource(audioFile.getAbsolutePath());
        player.prepare();
      } catch (Exception e) {
        throw new RuntimeException(
            "Exception in MediaPlayer.prepare", e);
      }

      statusTextView.setText("Ready to Play");

      playRecording.setEnabled(true);
      stopRecording.setEnabled(false);
      startRecording.setEnabled(true);
    } else if (v == startRecording) {
      recorder = new MediaRecorder();
      recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

      File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/files/");
      path.mkdirs();

      try {
        audioFile = File.createTempFile("recording", ".3gp", path);
      recorder.setOutputFile(audioFile.getAbsolutePath());
      } catch (Exception e) {
        throw new RuntimeException(
            "Exception on MediaRecorder.prepare", e);
      }
      recorder.start();
      statusTextView.setText("Recording");
      playRecording.setEnabled(false);
      stopRecording.setEnabled(true);
      startRecording.setEnabled(false);
    } else if (v == playRecording) {
      player.start();
      statusTextView.setText("Playing");
      playRecording.setEnabled(false);
      stopRecording.setEnabled(false);
      startRecording.setEnabled(false);
    }
  }

  public void onCompletion(MediaPlayer mp) {
    playRecording.setEnabled(true);
    stopRecording.setEnabled(false);
    startRecording.setEnabled(true);
    statusTextView.setText("Ready");
  }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/StatusTextView" android:text="Status" android:textSize="35dip"></TextView>

    <Button android:text="Start Recording" android:id="@+id/StartRecording" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Stop Recording" android:id="@+id/StopRecording" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Play Recording" android:id="@+id/PlayRecording" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/FinishButton" android:text="Finish"></Button>
</LinearLayout>

   
    
    
  








Related examples in the same category

1.Record audio to 3gpp file
2.Audio Recording
3.Audio recoding and call back
4.Using BackgroundAudioServiceBinder
5.Set Audio source and format
6.Audio processing library
7.Audio Synthesis
8.Audio Synthesis 440 Hz
9.Audio Files Filter
10.Recording Audio
11.AudioClip
12.Random Media Player