Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Recording Media


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.

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  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.     <Button
  11.         android:id=“@+id/button1”
  12.         android:layout_width=“wrap_content”
  13.         android:layout_height=“wrap_content”
  14.         android:layout_alignParentLeft=“true”
  15.         android:layout_alignParentTop=“true”
  16.         android:layout_marginLeft=“68dp”
  17.         android:layout_marginTop=“50dp”
  18.         android:text=“Start Recording”
  19.         android:onClick=“startRecording”
  20.          />
  21.     <Button
  22.         android:id=“@+id/button2”
  23.         android:layout_width=“wrap_content”
  24.         android:layout_height=“wrap_content”
  25.         android:layout_alignLeft=“@+id/button1”
  26.         android:layout_below=“@+id/button1”
  27.         android:layout_marginTop=“64dp”
  28.         android:text=“Stop Recording”
  29.         android:onClick=“stopRecording”
  30.         />
  31. </RelativeLayout>

Activity class

File: MainActivity.java
  1. package com.Kreationnext.mediarecorder;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import android.app.Activity;
  5. import android.content.ContentResolver;
  6. import android.content.ContentValues;
  7. import android.content.Intent;
  8. import android.media.MediaRecorder;
  9. import android.net.Uri;
  10. import android.os.Bundle;
  11. import android.os.Environment;
  12. import android.provider.MediaStore;
  13. import android.util.Log;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.Toast;
  17. public class MainActivity extends Activity {
  18.       MediaRecorder recorder;
  19.       File audiofile = null;
  20.       static final String TAG = “MediaRecording”;
  21.       Button startButton,stopButton;
  22.       @Override
  23.       public void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.         startButton = (Button) findViewById(R.id.button1);
  27.         stopButton = (Button) findViewById(R.id.button2);
  28.       }
  29.       public void startRecording(View view) throws IOException {
  30.         startButton.setEnabled(false);
  31.         stopButton.setEnabled(true);
  32.             //Creating file
  33.         File dir = Environment.getExternalStorageDirectory();
  34.         try {
  35.           audiofile = File.createTempFile(“sound”“.3gp”, dir);
  36.         } catch (IOException e) {
  37.           Log.e(TAG, “external storage access error”);
  38.           return;
  39.         }
  40.             //Creating MediaRecorder and specifying audio source, output format, encoder & output format
  41.         recorder = new MediaRecorder();
  42.         recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  43.         recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  44.         recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  45.         recorder.setOutputFile(audiofile.getAbsolutePath());
  46.         recorder.prepare();
  47.         recorder.start();
  48.       }
  49.       public void stopRecording(View view) {
  50.         startButton.setEnabled(true);
  51.         stopButton.setEnabled(false);
  52.             //stopping recorder
  53.         recorder.stop();
  54.         recorder.release();
  55.            //after stopping the recorder, create the sound file and add it to media library.
  56.         addRecordingToMediaLibrary();
  57.       }
  58.       protected void addRecordingToMediaLibrary() {
  59.             //creating content values of size 4
  60.         ContentValues values = new ContentValues(4);
  61.         long current = System.currentTimeMillis();
  62.         values.put(MediaStore.Audio.Media.TITLE, “audio” + audiofile.getName());
  63.         values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
  64.         values.put(MediaStore.Audio.Media.MIME_TYPE, “audio/3gpp”);
  65.         values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
  66.              //creating content resolver and storing it in the external content uri
  67.         ContentResolver contentResolver = getContentResolver();
  68.         Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  69.         Uri newUri = contentResolver.insert(base, values);
  70.            //sending broadcast message to scan the media file so that it can be available
  71.         sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
  72.         Toast.makeText(this“Added File “ + newUri, Toast.LENGTH_LONG).show();
  73.       }
  74.     }

Output:

recordingoutput1

recordingoutput2

Comments

comments