Put below code into your main Activity.
private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private MediaRecorder recorder = null;
private int currentFormat = 0;
private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4, MediaRecorder.OutputFormat.THREE_GPP };
private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setButtonHandlers();
enableButtons(false);
setFormatButtonCaption();
}
private void setButtonHandlers() {
((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);
((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);
((Button) findViewById(R.id.btnFormat)).setOnClickListener(btnClick);
}
private void enableButton(int id, boolean isEnable) {
((Button) findViewById(id)).setEnabled(isEnable);
}
private void enableButtons(boolean isRecording) {
enableButton(R.id.btnStart, !isRecording);
enableButton(R.id.btnFormat, !isRecording);
enableButton(R.id.btnStop, isRecording);
}
private void setFormatButtonCaption() {
((Button) findViewById(R.id.btnFormat)).setText(getString(R.string.audio_format) + " (" + file_exts[currentFormat] + ")");
}
private String getFilename() {
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);
if (!file.exists()) {
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
}
private void startRecording() {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats[currentFormat]);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopRecording() {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
}
private void displayFormatDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String formats[] = { "MPEG 4", "3GPP" };
builder.setTitle(getString(R.string.choose_format_title)).setSingleChoiceItems(formats, currentFormat, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
currentFormat = which;
setFormatButtonCaption();
dialog.dismiss();
}
}).show();
}
private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
Toast.makeText(AudioRecordingActivity.this, "Error: " + what + ", " + extra, Toast.LENGTH_SHORT).show();
}
};
private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
Toast.makeText(AudioRecordingActivity.this, "Warning: " + what + ", " + extra, Toast.LENGTH_SHORT).show();
}
};
private View.OnClickListener btnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart: {
Toast.makeText(AudioRecordingActivity.this, "Start Recording", Toast.LENGTH_SHORT).show();
enableButtons(true);
startRecording();
break;
}
case R.id.btnStop: {
Toast.makeText(AudioRecordingActivity.this, "Stop Recording", Toast.LENGTH_SHORT).show();
enableButtons(false);
stopRecording();
break;
}
case R.id.btnFormat: {
displayFormatDialog();
break;
}
}
}
};
Main.xml File:-
<?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"
android:padding="20dip">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/app_info"
android:layout_weight="1.0"
android:textSize="20dip"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnStart"
android:text="@string/start_recording"
android:layout_weight="1.0"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnStop"
android:text="@string/stop_recording"
android:layout_weight="1.0"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnFormat"
android:text="Format (mp4)"
android:layout_weight="1.0"/>
</LinearLayout>
</LinearLayout>
Add Below Permissions to manifest.xml file.
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Download Full Source Code from below link.
Voice Recording in Android
Don’t forget to provide feedback or follow this blog, if you find this blog is useful.
private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private MediaRecorder recorder = null;
private int currentFormat = 0;
private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4, MediaRecorder.OutputFormat.THREE_GPP };
private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setButtonHandlers();
enableButtons(false);
setFormatButtonCaption();
}
private void setButtonHandlers() {
((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);
((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);
((Button) findViewById(R.id.btnFormat)).setOnClickListener(btnClick);
}
private void enableButton(int id, boolean isEnable) {
((Button) findViewById(id)).setEnabled(isEnable);
}
private void enableButtons(boolean isRecording) {
enableButton(R.id.btnStart, !isRecording);
enableButton(R.id.btnFormat, !isRecording);
enableButton(R.id.btnStop, isRecording);
}
private void setFormatButtonCaption() {
((Button) findViewById(R.id.btnFormat)).setText(getString(R.string.audio_format) + " (" + file_exts[currentFormat] + ")");
}
private String getFilename() {
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);
if (!file.exists()) {
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
}
private void startRecording() {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats[currentFormat]);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopRecording() {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
}
private void displayFormatDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String formats[] = { "MPEG 4", "3GPP" };
builder.setTitle(getString(R.string.choose_format_title)).setSingleChoiceItems(formats, currentFormat, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
currentFormat = which;
setFormatButtonCaption();
dialog.dismiss();
}
}).show();
}
private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
Toast.makeText(AudioRecordingActivity.this, "Error: " + what + ", " + extra, Toast.LENGTH_SHORT).show();
}
};
private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
Toast.makeText(AudioRecordingActivity.this, "Warning: " + what + ", " + extra, Toast.LENGTH_SHORT).show();
}
};
private View.OnClickListener btnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart: {
Toast.makeText(AudioRecordingActivity.this, "Start Recording", Toast.LENGTH_SHORT).show();
enableButtons(true);
startRecording();
break;
}
case R.id.btnStop: {
Toast.makeText(AudioRecordingActivity.this, "Stop Recording", Toast.LENGTH_SHORT).show();
enableButtons(false);
stopRecording();
break;
}
case R.id.btnFormat: {
displayFormatDialog();
break;
}
}
}
};
Main.xml File:-
<?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"
android:padding="20dip">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/app_info"
android:layout_weight="1.0"
android:textSize="20dip"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnStart"
android:text="@string/start_recording"
android:layout_weight="1.0"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnStop"
android:text="@string/stop_recording"
android:layout_weight="1.0"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnFormat"
android:text="Format (mp4)"
android:layout_weight="1.0"/>
</LinearLayout>
</LinearLayout>
Add Below Permissions to manifest.xml file.
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Download Full Source Code from below link.
Voice Recording in Android
Don’t forget to provide feedback or follow this blog, if you find this blog is useful.
thanks
ReplyDeletehi nice tutorial. :) keep it up ...
ReplyDeletehow to view these media files in your app.?
ReplyDeleteThank you SO much for this piece of code !
ReplyDeleteI used several days for getting a good recording program up and running.
i applied this code but upon pressing stop button a dialog box saying "force close " will appear? how can i solve this? please help.
ReplyDeleteIf you are debugging directly into your phone disconnect the storage from your computer first...
DeleteHi Sarah, You solved this issue ?
DeleteI am also getting this issue but I don't know how to fix this ? Please advise..
thank you Dipak it was very useful
ReplyDeletehi there i want to save recording to file and convert it to mp3 is it same with normal java code ?I want to use JAVE library to convert the file
ReplyDeletehi can you help us by showing where your recordings are saved bcz my app using your code works but don't know whether it is recorded or not
ReplyDeleteI have already download this script and run my SDK but I am getting more error. from there
ReplyDeleteHello
ReplyDeleteYour code is really useful!
I’m really new in java programming. I want to edit your code, to make it to do some more things :
I want the application to be enabled by NFC, record sound (during sleep) continuously (in separated files of 1-2 minutes length and then start a new recording file), uploading each one of them (right after it’s recorded) on a server and then deleting them from the phone. Also I want to add a UTC timestamp to every file , to be able to synchronize these sounds recordings with other signals, on the same graph.
Could you help me with any of these? Any Help would be appreciated.
Thanks in advance!
What are all the formats Android can encode audio utilizing this code?
ReplyDeleteuseful 1....
ReplyDeleteAwesome, thanks a bunch!
ReplyDeleteThe link is broken. Can you update it? Thanks a lot!
ReplyDeleteI have tried this code, but only change is audio output format from "3gp" to "amr". I have test this code in tablet, code records audio fairly well but some time creates only 6 bytes file (only amr header). Can someone help me on this problem?
ReplyDeleteI downloaded the app, it is recording but the problem is that this app is interrupting my camera video recording and every app that is using audio record, does anyone know how can I add a workaround or any condition that will stop my app whenever other app uses the audio recorder?
ReplyDeletenot working!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ReplyDeletejava.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at com.dipak.audiorecording.AudioRecordingActivity.startRecording(AudioRecordingActivity.java:83)
nice tutorial. At present, I am using Total Recall auto call recorder app which records calls with crystal clear quality. Also i received excellent support from their end.
ReplyDeleteHi,
ReplyDeleteNice tutorial, working perfectly. I need to implement voice chat feature in my application. After lang search, i know it's possible using SIP, XMPP but i'm very confused with that. In everywhere video chat implementation available, but voice chat implementation not available. I want to know work flow of voice chat implementation, how recorded voice encode and send to server and at receiver how decode and listen that audio file.
If anyone knows the process please help me.
I want to know too, did you find out?
DeleteThanks Tani... i used Total Recall call recorder, its great yrr...! Recording quality very clear...thanks again for suggestion
ReplyDeleteGreat job!! Thank you for sharing code. it is very helpful for me. Thanks a lot! recording app
ReplyDeleteNice post, but Total recall recording app also great...
ReplyDeleteThanks for the code . could someone maybe help me with adding a send button so the recording could be accessible in a central location
ReplyDeleteyou're welcome, just contact me
DeleteIt's really useful code but we have to care of those person who can never put this code in activity.
ReplyDeleteSo Now HALF OFF for a very limited time. Half off price is reflected in the price shown
Total Recall android call recorder is free for 90 days, then will be limited to recording calls for 1 minute until purchased.
... evoicerecorder.blogspot.com
ReplyDeleteI have converted MIC to VOICE_CALL for recording purpose but its only taking MIC voice in call is there any solution so that i cn get both side voice
ReplyDeleteThanks for share the calls recording ideas , nice post .
ReplyDeleteThanks for sharing this information with us.
ReplyDeleteAudio Rental Washington DC
Recording Equipment Rental
I want the recorder to stop after 1min without any stop button..how can i do that?
ReplyDeleteTry the Handler Concept. Run() Method based Stop the Record after the 1 min Record.
Deletenice code
ReplyDeletehi am record 31 sec .mp4 file and using setAudioSamplingRate(8000,11025,22050,44100) but every time file size same. how to change file size.
ReplyDeletevoice recorder is a convenient and simple tool that can be used for audio recorder and voice recording. Their are many new features comes in voice recorder which help you in recording the voice.Heinrich limited provide high qualitative products of safety and technology.
ReplyDeleteCheck this call recorder App it also fail to work in many devices i am trying to make it work for motorola devices but its not supporting
ReplyDeleteI really wanted to develop a brief comment so as to say thanks to you for all the nice pointers you are sharing here. My extensive internet look up has at the end of the day been honored with reputable content to write about with my contacts. I ‘d admit that most of us visitors actually are undeniably lucky to be in a remarkable network with many lovely professionals with useful hints. I feel somewhat lucky to have come across the webpage and look forward to many more excellent moments reading here. Thank you once again for everything.
ReplyDeleteDigital Brief Sydney
I am doing an online photography course and this site has provided me with new photography insights and ideas that I will use to improve my knowledge and skills. thanks so much for sharing this article with us and in case you need article and content writing services, do not hesitate to visit our site by clicking on University Essays Writing Service.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteWe understand that students in Australia encounter different challenges in completing their Best Research Paper Writing Service. We offer College Term Paper Writing Service to students regardless of their specialty, discipline or educational level.
ReplyDeleteUpgrade your surveillance game with the top mini voice recorder in India from Spy World. Shop now for the best deals on cutting-edge spy devices!
ReplyDeleteThis is such an insightful post about voice recording on Android! It’s incredible how this technology has evolved and is now used in various industries. One thing that came to mind is how voice recording can be useful for business assignment writing service Imagine students or professionals recording their ideas or important discussions on the go and then using these recordings to structure their assignments later. It could save a lot of time and ensure no valuable points are missed. Such a smart way to integrate technology into academic or business tasks!
ReplyDeleteIf you need solid Dissertation Proposal Writing Services, I can’t recommend Dissertation Help Services enough! They’re really great at breaking down the process and helping you create a proposal that stands out. The team is friendly and responsive, making it a breeze to get the support you need. It’s definitely worth checking out if you want to make your proposal shine!
ReplyDeleteIf you’re working on international deals, connecting with Letter of Credit Experts USA can really make a difference! Super Fast Document Company is a fantastic resource for getting the guidance you need. Their knowledgeable team is ready to help you navigate the complexities, making the process smooth and stress-free. With their expertise, you’ll feel more confident in your transactions!
ReplyDelete