Set Audio source and format : Audio « Media « Android






Set Audio source and format

   

package app.test;

import java.io.File;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaRecorder;
import android.os.AsyncTask;
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, amplitudeTextView;
  Button startRecording, stopRecording, playRecording, finishButton;
  MediaRecorder recorder;
  MediaPlayer player;
  File audioFile;

  RecordAmplitude recordAmplitude;
  boolean isRecording = false;

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

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

    amplitudeTextView = (TextView) this.findViewById(R.id.AmplitudeTextView);
    amplitudeTextView.setText("0");

    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) {
      isRecording = false;
      recordAmplitude.cancel(true);

      recorder.stop();
      recorder.release();

      player = new MediaPlayer();
      player.setOnCompletionListener(this);
      try {
        player.setDataSource(audioFile.getAbsolutePath());
        player.prepare();
      } catch (Exception e) {
        throw new RuntimeException("IOException 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());
        recorder.prepare();
      } catch (Exception e) {
        throw new RuntimeException(
            "IOException on MediaRecorder.prepare", e);
      }
      recorder.start();
      isRecording = true;
      recordAmplitude = new RecordAmplitude();
      recordAmplitude.execute();
      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");
  }

  private class RecordAmplitude extends AsyncTask<Void, Integer, Void> {
    @Override
    protected Void doInBackground(Void... params) {
      while (isRecording) {
        try {
          Thread.sleep(500);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        publishProgress(recorder.getMaxAmplitude());
      }
      return null;
    }
    protected void onProgressUpdate(Integer... progress) {
      amplitudeTextView.setText(progress[0].toString());
    }
  }
}



//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>
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/AmplitudeTextView" android:textSize="35dip" android:text="0"></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.Custom Audio Recorder
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