How to Do Audio Recording in Android Using Java

From WikiHTP

The easiest way to capture audio on Android is through the recorder provided by the Android operating system. We invoke the recording application and then recover the recorded audio.

It has the advantage that the interface is familiar to the user since many applications use this feature.

Create Audio Recording[edit]

Arrange two objects of the Button class with the labels "record" and "play". When the record button is pressed, proceed to activate the recorder provided by Android. When the play button is pressed, play the recorded audio.

Create a project and implement the interface, initialize the onClick events of each button.

package com.wikihtp.audio;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    int request = 1;
    Uri url1;

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

    public void record (View v) {
        Intent intent = new Intent (MediaStore.Audio.Media.RECORD_SOUND_ACTION);
        startActivityForResult (intent, request);
    }

    public void play (View v) {
        MediaPlayer mediaPlayer = MediaPlayer.create (this, url1);
        mediaPlayer.start ();
    }

    protected void onActivityResult (int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == request) {
            url1 = data.getData ();
        }
    }
}

When the button to record the audio is pressed through an Intent we activate the Android's own recording application.

Next, we call the startActivityForResult method to be able to recover the recording after it is finished through the onActivityResult method.

    public void record (View v) {
        Intent intent = new Intent (MediaStore.Audio.Media.RECORD_SOUND_ACTION);
        startActivityForResult (intent, request);
    }

We must pass to the startActivityForResult method in addition to the Intent reference a variable with a value 0 or positive (then this value will return to the onActivityResult method).

When we finish recording, the onActivityResult method is executed, where we store in the variable url1 the reference to the created audio file.

    protected void onActivityResult (int requestCode, int resultCode, Intent data) {
    	if (resultCode == RESULT_OK && requestCode == request) {
    	url1 = data.getData ();
    	}
    }

Finally to execute the content of the recording we use the class already seen MediaPlayer.

    public void play (View v) {
        MediaPlayer mediaPlayer = MediaPlayer.create (this, url1);
        mediaPlayer.start ();       
    }

About This Tutorial

This page was last edited on 19 February 2019, at 18:05.