Wednesday 9 March 2016

A Simple Android Recorder

Android has provided many different Media classes that are able to record media(audio) and play them one among them is the MediaRecorder Class.
In this post I'm going walk you through a recording app that uses MediaRecorder class.

Lets just start with designing a basic xml layout of the recording Activity
You can download the entire code here.

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin"  
     android:paddingTop="@dimen/activity_vertical_margin"
     tools:context=".MainActivity" >

        <Button 
             android:id="@+id/btntoggelRec" 
             android:layout_width="0dp"
             android:layout_height="80dp" 
             android:layout_weight="1"
             android:text="Record" 
             android:textAppearance="?android:attr/textAppearanceLarge" 
             android:textStyle="bold" />
        <Button 
             android:id="@+id/btnPlayRec"
             android:layout_width="0dp" 
             android:layout_height="80dp" 
             android:layout_weight="1" 
             android:text="Play" 
             android:textAppearance="?android:attr/textAppearanceLarge"
             android:textStyle="bold" />

</LinearLayout>

after using the above code you should have a layout as follows


as you see we have two buttons one for the recording start/stop and other for playback start/stop.

the next step now would be start with the Java code

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private MediaRecorder audioRecorder;
    private boolean mIsRecording;
    private boolean isRecording;
    private boolean isPlaying;
    private MediaPlayer player;

    @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button record = (Button) findViewById(R.id.btntoggelRec);
        Button playBack = (Button) findViewById(R.id.btnPlayRec);

        // set recording boolean flag to false 
        isRecording = false;
        // set playing flag to false 
        isPlaying = false;

        //get the output file where you want the recording to be stored 
        final String outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";

        // step 1 : setup MediaRecorder
        audioRecorder = new MediaRecorder();
        audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        audioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        audioRecorder.setOutputFile(outputFile);
        // End step 1
        //step 2 : setup recording start/stop 
        record.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                if(isRecording){
                    Toast.makeText(MainActivity.this,"Recording Stoped",Toast.LENGTH_SHORT).show();
                    audioRecorder.stop();
                    audioRecorder.release();
                    isRecording=false;
                    ((Button)v).setText("Record");


                }else {
                    try {
                        Toast.makeText(MainActivity.this,"Recording Started",Toast.LENGTH_SHORT).show();
                        audioRecorder.prepare();
                        audioRecorder.start();
                        isRecording=true;
                        ((Button)v).setText("Stop");
                    }catch (Exception ex){
                        Toast.makeText(MainActivity.this,"Recording Error",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        // end of step 2
        player = new MediaPlayer();

        // step 3 : setup playback start/Stop
        playBack.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v) {
                if(isPlaying){
                    player.stop();
                    player.release();
                    isPlaying = false;
                    ((Button)v).setText("Play");
                }else {

                    try {
                        player.setDataSource(outputFile);
                        player.prepare();
                        player.start();
                        isPlaying=true;
                        ((Button)v).setText("Stop");
                        Toast.makeText(MainActivity.this, "PlayBack Started", Toast.LENGTH_SHORT).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                        Toast.makeText(MainActivity.this,"No recording to play please record first",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        // end of step 3



    }
}

as you see the code I have  marked the start and end of steps I will explain each of them now
Step 1: Setup MediaRecorder
in this step I have created a MediaRecorder object and then initilized it with some compulsory fields that are
  1. source of audio
  2. output format
  3. audio encoder
  4. the path of the audio file to be stored
Step 2 : Setup Recording Start/Stop 
This step has a button click listener in which the recording is toggled according to the boolean flag (isRecording). If false start recording if true stop. Notice the exceptions thrown by the MediaRecorder functions.

Step 3 : Setup Playback Start/Stop
This step has smiler a button click listener in which the playback is toggled according to the boolean flag (isPlaying). If false start recording if true stop.
notice the class that is used to play is MediaPlayer class.

the last thing now you will have to do to get app runnung is adding the permissions in the manifest file in order to access the mic and the storage these permissions are as follows.

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

well no no can go ahead and try to run your recorder app.
You can download the entire code here.

In the next post we will extend this app to a little more features like visualization
Please feel free to comment.

1 comment:

  1. The 20 Best Casino Names in Las Vegas, NV | MapyRO
    From penny slots 논산 출장안마 to high rollers, we 성남 출장안마 list 20 casinos in 화성 출장안마 Las Vegas. 아산 출장마사지 the best for the best slot machines, but at some point will get you an even better idea of 부천 출장마사지

    ReplyDelete