Build a Audio or Sound Recorder Android App in Android Studio Using Java Full Project For Beginners

 

Welcome folks today in this blog post we will be building an audio or sound recorder android app in android studio using java.All the full source code of the application is shown below.

 

 

 

Get Started

 

 

 

java code

 

 

package com.example.myaudiorecorder.myaudiorecorder;

import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.io.File;

public class MainActivity extends AppCompatActivity {

MediaRecorder mediaRecorder;

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

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, PackageManager.PERMISSION_GRANTED);

mediaRecorder = new MediaRecorder();
}

public void startRecording(View view){

try {

mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, “/YouTubeAudio.3gp”);

mediaRecorder.setOutputFile(file);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

mediaRecorder.prepare();
mediaRecorder.start();

}
catch (Exception e){
e.printStackTrace();
}
}

public void stopRecording(View view){

mediaRecorder.stop();
mediaRecorder.release();

}

}

 

 

manifest file

 

 

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.myaudiorecorder.myaudiorecorder”>

<uses-permission android:name=”android.permission.RECORD_AUDIO”/>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_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>

 

See also  How to Share Files or Text on Button Click on Gmail, Whatsapp, SMS, Bluetooth in Android App Using Android Studio and Java Full Project For Beginners

 

xml file

 

 

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”com.example.myaudiorecorder.myaudiorecorder.MainActivity”>

<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentStart=”true”
android:layout_alignParentTop=”true”
android:layout_marginStart=”129dp”
android:layout_marginTop=”93dp”
android:onClick=”startRecording”
android:text=”@string/start” />

<Button
android:id=”@+id/button2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignStart=”@+id/button”
android:layout_below=”@+id/button”
android:layout_marginTop=”146dp”
android:onClick=”stopRecording”
android:text=”@string/stop” />
</RelativeLayout>

Leave a Reply