Easy Tutorial
❮ Programming Intro Reg Lookahead Lookbehind ❯

9.4 Using MediaRecord for Audio Recording

Category Android Basic Tutorial

Introduction to This Section

This section is the final part of the basic API calls for Android multimedia, introducing the simple use of MediaRecord. The usage is very straightforward; let's write an example to get familiar with it.


1. Recording Audio with MediaRecord

Running Result :

Implementation Code :

Layout Code: activity_main.xml :

<RelativeLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_control"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Recording" />

</RelativeLayout>

MainActivity.java :

public class MainActivity extends AppCompatActivity {

    private Button btn_control;
    private boolean isStart = false;
    private MediaRecorder mr = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_control = (Button) findViewById(R.id.btn_control);
        btn_control.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isStart){
                    startRecord();
                    btn_control.setText("Stop Recording");
                    isStart = true;
                }else{
                    stopRecord();
                    btn_control.setText("Start Recording");
                    isStart = false;
                }
            }
        });
    }

    // Start recording
    private void startRecord(){
        if(mr == null){
            File dir = new File(Environment.getExternalStorageDirectory(),"sounds");
            if(!dir.exists()){
                dir.mkdirs();
            }
            File soundFile = new File(dir,System.currentTimeMillis()+".amr");
            if(!soundFile.exists()){
                try {
                    soundFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            mr = new MediaRecorder();
            mr.setAudioSource(MediaRecorder.AudioSource.MIC);  // Audio source
            mr.setOutputFormat(MediaRecorder.OutputFormat.AMR_WB);   // Set output format
            mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);   // Set encoding format
            mr.setOutputFile(soundFile.getAbsolutePath());
            try {
                mr.prepare();
                mr.start();  // Start recording
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    // Stop recording and release resources
    private void stopRecord(){
        if(mr != null){
            mr.stop();
            mr.release();
            mr = null;
        }
    }
}

Don't forget to add the following permissions in AndroidManifest.xml:

&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
&lt;uses-permission android:name="android.permission.RECORD_AUDIO"/>

That's it, it's that simple!


2. Download the Example Code for This Section

RecordDemo.zip


Summary of This Section:

>

Well, the content of this section is very simple, it's just the use of MediaRecorder, probably the most concise section of the entire tutorial.

-1.0 Android Basic Tutorial

-1.0.1 Latest Android Basic Tutorial Catalog for 2015

-1.1 Background and System Architecture Analysis

-1.2 Development Environment Setup

-1.2.1 Developing Android APPs with Eclipse + ADT + SDK

-1.2.2 Developing Android APPs with Android Studio

-1.3 Solving SDK Update Issues

-1.4 Genymotion Emulator Installation

-1.5.1 Git Tutorial on Basic Operations of Local Repositories

-1.5.2 Git: Using GitHub to Set Up a Remote Repository

-1.6 How to Play with 9 (Jiu Mei) Images

-1.7 Interface Prototype Design

-1.8 Project-Related Analysis (Various Files, Resource Access)

❮ Programming Intro Reg Lookahead Lookbehind ❯