Build an Audio or Sound Recorder Android App in Android Studio Using Java

Table of Contents

Construct a Sound or Sound Recorder Android Application in Android Studio Utilizing Java Article has numerous implications. We will know this subject plainly through this article. We want to believe that you like this article.

Build an Audio or Sound Recorder Android App in Android Studio Using Java

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();

}

}
<?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>
<?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>

Final Words

Fabricate a Sound or Sound Recorder Android Application in Android Studio Utilizing Java We got to know our subject plainly. Furthermore, assuming that you feel somewhat uncertain about this article you can tell us your questions by means of remark.

Leave a Comment