AdSense

AdSense3

Wednesday, 14 October 2015

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.
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.   
  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: MainActivity.java
  1. package com.javatpoint.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.   
  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.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.         startButton = (Button) findViewById(R.id.button1);  
  29.         stopButton = (Button) findViewById(R.id.button2);  
  30.       }  
  31.   
  32.       public void startRecording(View view) throws IOException {  
  33.         startButton.setEnabled(false);  
  34.         stopButton.setEnabled(true);  
  35.             //Creating file  
  36.         File dir = Environment.getExternalStorageDirectory();  
  37.         try {  
  38.           audiofile = File.createTempFile("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.         recorder.setAudioSource(MediaRecorder.AudioSource.MIC);  
  46.         recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);  
  47.         recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);  
  48.         recorder.setOutputFile(audiofile.getAbsolutePath());  
  49.         recorder.prepare();  
  50.         recorder.start();  
  51.       }  
  52.   
  53.       public void stopRecording(View view) {  
  54.         startButton.setEnabled(true);  
  55.         stopButton.setEnabled(false);  
  56.             //stopping recorder  
  57.         recorder.stop();  
  58.         recorder.release();  
  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 = System.currentTimeMillis();  
  67.         values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());  
  68.         values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));  
  69.         values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");  
  70.         values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());  
  71.            
  72.              //creating content resolver and storing it in the external content uri  
  73.         ContentResolver contentResolver = getContentResolver();  
  74.         Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;  
  75.         Uri newUri = contentResolver.insert(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.         Toast.makeText(this"Added File " + newUri, Toast.LENGTH_LONG).show();  
  80.       }  
  81.     }   

Output:

android media recorder example output 1 android media recorder example output 2

No comments:

Post a Comment