Recording Media


Recording Media

In the android operating system, the MediaRecorder class allows the recording of audio or video files.  

In order to record an audio file audio, you need to use microphone of the device alongside MediaRecorder class. On the contrary, for video recording, we require to use the video camera with MediaRecorder class. In this blog, we will give you an example of audio recording in an android device.

Code 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="match_parent"     android:layout_height="match_parent">     <Button         android:id="@+id/btnRecord"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginLeft="100dp"         android:layout_marginTop="120dp"         android:text="Start Recording" />     <Button         android:id="@+id/btnStop"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginLeft="100dp"         android:text="Stop Recording" />     <Button         android:id="@+id/btnPlay"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginLeft="100dp"         android:text="Play Recording" />     <Button         android:id="@+id/btnStopPlay"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginLeft="100dp"         android:text="Stop Playing" /> </LinearLayout>

Now open your main activity file MainActivity.java from \java\com.tutlane.audiorecorderexample path and write the code like as shown below: MainActivity.java

package com.tutlane.audiorecorderexample; import android.content.pm.PackageManager; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Environment; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.io.IOException; import static android.Manifest.permission.RECORD_AUDIO; import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; public class MainActivity extends AppCompatActivity {     private Button startbtn, stopbtn, playbtn, stopplay;     private MediaRecorder mRecorder;     private MediaPlayer mPlayer;     private static final String LOG_TAG = "AudioRecording";     private static String mFileName = null;     public static final int REQUEST_AUDIO_PERMISSION_CODE = 1;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         startbtn = (Button)findViewById(R.id.btnRecord);         stopbtn = (Button)findViewById(R.id.btnStop);         playbtn = (Button)findViewById(R.id.btnPlay);         stopplay = (Button)findViewById(R.id.btnStopPlay);         stopbtn.setEnabled(false);         playbtn.setEnabled(false);         stopplay.setEnabled(false);         mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();         mFileName += "/AudioRecording.3gp";         startbtn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 if(CheckPermissions()) {                     stopbtn.setEnabled(true);                     startbtn.setEnabled(false);                     playbtn.setEnabled(false);                     stopplay.setEnabled(false);                     mRecorder = new MediaRecorder();                     mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);                     mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);                     mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);                     mRecorder.setOutputFile(mFileName);                     try {                         mRecorder.prepare();                     } catch (IOException e) {                         Log.e(LOG_TAG, "prepare() failed");                     }                     mRecorder.start();                     Toast.makeText(getApplicationContext(), "Recording Started", Toast.LENGTH_LONG).show();                 }                 else                 {                     RequestPermissions();                 }             }         });         stopbtn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 stopbtn.setEnabled(false);                 startbtn.setEnabled(true);                 playbtn.setEnabled(true);                 stopplay.setEnabled(true);                 mRecorder.stop();                 mRecorder.release();                 mRecorder = null;                 Toast.makeText(getApplicationContext(), "Recording Stopped", Toast.LENGTH_LONG).show();             }         });         playbtn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 stopbtn.setEnabled(false);                 startbtn.setEnabled(true);                 playbtn.setEnabled(false);                 stopplay.setEnabled(true);                 mPlayer = new MediaPlayer();                 try {                     mPlayer.setDataSource(mFileName);                     mPlayer.prepare();                     mPlayer.start();                     Toast.makeText(getApplicationContext(), "Recording Started Playing", Toast.LENGTH_LONG).show();                 } catch (IOException e) {                     Log.e(LOG_TAG, "prepare() failed");                 }             }         });         stopplay.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 mPlayer.release();                 mPlayer = null;                 stopbtn.setEnabled(false);                 startbtn.setEnabled(true);                 playbtn.setEnabled(true);                 stopplay.setEnabled(false);                 Toast.makeText(getApplicationContext(),"Playing Audio Stopped", Toast.LENGTH_SHORT).show();             }         });     }     @Override     public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {         switch (requestCode) {             case REQUEST_AUDIO_PERMISSION_CODE:                 if (grantResults.length> 0) {                     boolean permissionToRecord = grantResults[0] == PackageManager.PERMISSION_GRANTED;                     boolean permissionToStore = grantResults[1] ==  PackageManager.PERMISSION_GRANTED;                     if (permissionToRecord && permissionToStore) {                         Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_LONG).show();                     } else {                         Toast.makeText(getApplicationContext(),"Permission Denied",Toast.LENGTH_LONG).show();                     }                 }                 break;         }     }     public boolean CheckPermissions() {         int result = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_EXTERNAL_STORAGE);         int result1 = ContextCompat.checkSelfPermission(getApplicationContext(), RECORD_AUDIO);         return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED;     }     private void RequestPermissions() {         ActivityCompat.requestPermissions(MainActivity.this, new String[]{RECORD_AUDIO, WRITE_EXTERNAL_STORAGE}, REQUEST_AUDIO_PERMISSION_CODE);     } }   For setting permission in the manifest file, recording and saving the audio file, click the android manifest file (AndroidManifest.xml). Then, open it to write the code. AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.tutlane.audiorecorderexample">     <uses-permission android:name="android.permission.RECORD_AUDIO"/>     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>     <uses-permission android:name="android.permission.STORAGE"/>     <application         android:allowBackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:roundIcon="@mipmap/ic_launcher_round"         android:supportsRtl="true"         android:theme="@style/AppTheme">         <activity android:name=".MainActivity">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                 <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>     </application> </manifest>

Image

Image Not Found