1/ Create A New Project Add xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="340dp"
android:text="Record"
android:onClick="btnRecordPressed"
app:layout_constraintEnd_toStartOf="@+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="340dp"
android:text="Stop"
android:onClick="btnStopPressed"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="340dp"
android:text="Play"
android:onClick="btnPlayPressed"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2/ Gave permission for Record Audio
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
3/ MainActivity.Java.
package com.tutorialb.recordsaudioandsave;
import android.content.ContextWrapper;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private static int MICROPHONE_PERMISSION_CODE = 200;
MediaRecorder mediaRecorder;
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
if (isMicrophonePresent()) {
getMicrophonePermission();
}
}
public void btnRecordPressed (View v) {
try {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(getRecordingFilePath());
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.prepare();
mediaRecorder.start();
Toast.makeText(this, "Recording is started", Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void btnStopPressed (View v) {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
Toast.makeText(this, "Recording is stopped", Toast.LENGTH_SHORT).show();
}
public void btnPlayPressed (View v) {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(getRecordingFilePath());
mediaPlayer.prepare();
mediaPlayer.start();
Toast.makeText(this, "Recording is playing", Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
}
}
private boolean isMicrophonePresent() {
if (this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MICROPHONE)) {
return true;
}
else {
return false;
}
}
private void getMicrophonePermission() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.RECORD_AUDIO)
== PackageManager.PERMISSION_DENIED){
ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.RECORD_AUDIO}, MICROPHONE_PERMISSION_CODE);
}
}
private String getRecordingFilePath() {
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File musicDirectory = contextWrapper.getExternalFilesDir(Environment.DIRECTORY_MUSIC);
File file = new File(musicDirectory, "testRecorddingFile" + ".mp3");
return file.getPath();
}
}