To record sound in android we have MediaRecorder class. It is used to record audio and video.
Its is a very flexible class ,it lets you set the encoding type, audio source type , output type etc
The example bellow will explain the usage of MediaRecorder class and saving it in sd card if available.
PERMISSIONS REQUIRED
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>
<uses-permission android:name=”android.permission.RECORD_AUDIO” />
Step 1.
Set the path where we wish to store the recoreded sound file.
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + “/audioRecord/record.3gp”;
Environment.getExternalStorageDirectory().getAbsolutePath() returns the absolute path of he sdcard .This path refers to audioRecord folder in sdcard that will contain the recourded sound file in .3gp format.We can use other formats also like mp3,wav etc .
Step 2.
Creating and using MediaRecorder class object
MediaRecorder recorder = new MediaRecorder();
//Sets the audio source to be used for recording.
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//setting 3gp as output format recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//setting encoding format recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);//path of the audio recording to be stored
Step 3
Before we start recording ,we check the availability of sd card.If sd card is mounted then we start the recording.
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED))
{
recorder.prepare();//Prepares the recorder to begin
recorder.start();//begins capturing data
}
Step 4
Stop recording and release the resources in the following way .
recorder.stop();//stops recording
recorder.release();//releases resources
This is how we record and save recorded sound in android.
Here’s the complete example and recordDemo project for your reference.
[sourcecode language=”java”]import android.os.Bundle;
import android.app.Activity;
import android.view.View.OnClickListener;
import java.io.File;
import java.io.IOException;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
//records and saves a sound in sd card
File audiofile = null;
private static final String TAG = “SoundRecordingActivity”;
Button startButton, stopButton;
String path;
MediaRecorder recorder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = (Button) findViewById(R.id.start);
stopButton = (Button) findViewById(R.id.stop);
startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
recorder = new MediaRecorder();
path=Environment.getExternalStorageDirectory().getAbsolutePath() + “/audioRecord/record.3gp”;
}
public void onClick(View v)
{
Log.i(“hi”, “in onclick() of main Act.”);
switch (v.getId()) {
case R.id.start:
try {
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
Log.i(“hi”, “SD Card is not mounted” );
}
// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
Log.i(“hi”,”Path to file could not be created.”);
}
//Sets the audio source to be used for recording.
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//setting 3gp as output format
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
/setting encoding format
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//path of the audio recording to be stored
recorder.setOutputFile(path);
//Prepares the recorder to begin
recorder.prepare();
//begins capturing data
recorder.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.stop:
try {
recorder.stop();//stops recording
recorder.release();//releases resources
//recorder1.stop();//stops the recording
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}// class
[/sourcecode]
Screen shots of the above example :
record.3gp saved in audioRecord folder on sd card
Download the example project here.
Comments are closed.