Android MediaRecorder Example
MediaRecorder class can be used to record audio and video files.
After recording the media, we can create a sound file that can be played later.
In this example, we are going to record the audio file and storing it in the external directory in 3gp format.
activity_main.xml
Drag 2 buttons from the pallete, one to start the recording and another stop the recording. Here, we are registering the view with the listener in xml file using android:onClick.
Skip Ad
File: activity_main.xml
1. <RelativeLayout xmlns:androclass="[Link]
2. xmlns:tools="[Link]
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:paddingBottom="@dimen/activity_vertical_margin"
6. android:paddingLeft="@dimen/activity_horizontal_margin"
7. android:paddingRight="@dimen/activity_horizontal_margin"
8. android:paddingTop="@dimen/activity_vertical_margin"
9. tools:context=".MainActivity" >
10.
11. <Button
12. android:id="@+id/button1"
13. android:layout_width="wrap_content"
14. android:layout_height="wrap_content"
15. android:layout_alignParentLeft="true"
16. android:layout_alignParentTop="true"
17. android:layout_marginLeft="68dp"
18. android:layout_marginTop="50dp"
19. android:text="Start Recording"
20. android:onClick="startRecording"
21. />
22.
23. <Button
24. android:id="@+id/button2"
25. android:layout_width="wrap_content"
26. android:layout_height="wrap_content"
27. android:layout_alignLeft="@+id/button1"
28. android:layout_below="@+id/button1"
29. android:layout_marginTop="64dp"
30. android:text="Stop Recording"
31. android:onClick="stopRecording"
32. />
33.
34. </RelativeLayout>
Activity class
File: [Link]
1. package [Link];
2. import [Link];
3. import [Link];
4. import [Link];
5. import [Link];
6. import [Link];
7. import [Link];
8. import [Link];
9. import [Link];
10. import [Link];
11. import [Link];
12. import [Link];
13. import [Link];
14. import [Link];
15. import [Link];
16. import [Link];
17.
18. public class MainActivity extends Activity {
19. MediaRecorder recorder;
20. File audiofile = null;
21. static final String TAG = "MediaRecording";
22. Button startButton,stopButton;
23.
24. @Override
25. public void onCreate(Bundle savedInstanceState) {
26. [Link](savedInstanceState);
27. setContentView([Link].activity_main);
28. startButton = (Button) findViewById([Link].button1);
29. stopButton = (Button) findViewById([Link].button2);
30. }
31.
32. public void startRecording(View view) throws IOException {
33. [Link](false);
34. [Link](true);
35. //Creating file
36. File dir = [Link]();
37. try {
38. audiofile = [Link]("sound", ".3gp", dir);
39. } catch (IOException e) {
40. Log.e(TAG, "external storage access error");
41. return;
42. }
43. //Creating MediaRecorder and specifying audio source, output format, encoder & output format
44. recorder = new MediaRecorder();
45. [Link]([Link]);
46. [Link]([Link].THREE_GPP);
47. [Link]([Link].AMR_NB);
48. [Link]([Link]());
49. [Link]();
50. [Link]();
51. }
52.
53. public void stopRecording(View view) {
54. [Link](true);
55. [Link](false);
56. //stopping recorder
57. [Link]();
58. [Link]();
59. //after stopping the recorder, create the sound file and add it to media library.
60. addRecordingToMediaLibrary();
61. }
62.
63. protected void addRecordingToMediaLibrary() {
64. //creating content values of size 4
65. ContentValues values = new ContentValues(4);
66. long current = [Link]();
67. [Link]([Link], "audio" + [Link]());
68. [Link]([Link].DATE_ADDED, (int) (current / 1000));
69. [Link]([Link].MIME_TYPE, "audio/3gpp");
70. [Link]([Link], [Link]());
71.
72. //creating content resolver and storing it in the external content uri
73. ContentResolver contentResolver = getContentResolver();
74. Uri base = [Link].EXTERNAL_CONTENT_URI;
75. Uri newUri = [Link](base, values);
76.
77. //sending broadcast message to scan the media file so that it can be available
78. sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
79. [Link](this, "Added File " + newUri, Toast.LENGTH_LONG).show();
80. }
81. }
download this mediarecorder Example
Output: