How to build an Android Sound Recorder App - Hive programmers

avatar

Greetings to my favorite Science community online.

It's @skyehi and as always, it's a very thrilling feeling to be back to continue my favorite series on Android App development tutorials for beginners.

I must say that I'm really proud of the number of apps I'v been able to teach on how to build. We built a basic map application in our previous blog and we have built really awesome apps like an SMS app, a calculator, a camera and a memo app.

Through our attempts to develop these apps, I have been able to introduce my readers to different Android APIs and Java methods or functions.

I believe that App development is the best and most fun way of teaching beginners how to code or write computer programmers.

For today's blog, we're going to be working on an App that will use audio functions and APIs. We'll be building an Android Audio Recorder App.

At the end of this tutorial, you'll be able to successfully run the application and see it right on your physical Android device or an emulator on your computer.

Polish_20231126_174036509.jpgOriginal Image Source by Pexels from Pixabay

Let's begin our work for today shall we.

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Setting Up Your Project

As always, in order to be able to go through with this tutorial and build the Android App, you would need to make sure that Android Studio and Java Development kit is installed.

If you're having trouble installing both softwares, please let me know in the comments section below and I'll be of assistance.

Now I'll assume that my readers are through with this step and have successfully installed Android Studio.

To begin building our Android Audio Recorder, we would need to create a new Android Studio Project by
opening Android Studio and clicking on "Start a new Android Studio project,"

I usually don't mention this step because it seems obvious but the next thing to do is to choose a "Phone and Tablet" template.

As we do in every tutorial, I would recommend that you use "Empty Activity" as the template. This is to avoid further complicating the codes.

We would also need to set up the project details such as name, package name, and the save location. Also, please ensure that you choose Java as the programming language to use and not Kotlin. We'll be using Kotlin in future projects but for the basics, I will recommend Java.

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Designing Our User Interface

Alright guys, now that we're through with setting up the App Project in Android Studio, it's time to work on the first part of our work, the design part.

This is usually my favorite part when I'm working on complex Apps. This App however would not be a complex one since it's a tutorial for beginners.

Our design will be done in the res/layout/activity_main.xml file. So open that file and design the layout with all the required UI elements.

The User Interface design of our Audio Recorder App would include a Button for recording and a TextView to display the recording status.

Here's how the design layout code should look like;

<?xml version="1.0" encoding="utf-8"?>
<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/btnRecord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Record"
        android:layout_centerInParent="true"/>

    <TextView
        android:id="@+id/tvStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnRecord"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:text="Press record to start recording"/>

</RelativeLayout>

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Handling the Recording Logic of Our App

Now that we're done with the main design of our App which is the frontend that the user would interact with. It's time to write the code for the logic of the App. This code is what would make our Audio recoding App capable of recoding sounds.

This step will be done in the MainActivity.java . This is going to be a bit of a long code so brace yourselves guys.

We'll be using the MediaRecorder class and methods. We'll also include and override other methods. The record button we designed in the activity_main.xml page will be given its function in the code below.

Here's how the entire code should look like.

import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private MediaRecorder mediaRecorder;
    private String outputFile;

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

        final Button btnRecord = findViewById(R.id.btnRecord);
        final TextView tvStatus = findViewById(R.id.tvStatus);

        outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";

        mediaRecorder = new MediaRecorder();

        btnRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (btnRecord.getText().equals("Record")) {
                    startRecording();
                    btnRecord.setText("Stop");
                    tvStatus.setText("Recording...");
                } else {
                    stopRecording();
                    btnRecord.setText("Record");
                    tvStatus.setText("Recording Stopped");
                }
            }
        });
    }

    private void startRecording() {
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        mediaRecorder.setOutputFile(outputFile);

        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void stopRecording() {
        mediaRecorder.stop();
        mediaRecorder.release();
        mediaRecorder = null;
    }
}

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Requesting Record Audio Permissions

At this point of our tutorial, if you've been following my series, you would already know that if we require to use any service in Android like storage or internet, we would need to request permission in AndroidManifest.xml file.

For our audio recorder App to be a able to record sound using the microphone of the Android device, we need to include the Record Audio Permission.

Here's how the code should look in Manifest file

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Testing Your App

Congratulations guys, we have successfully built the awesome and simple Android Audio Recorder App.

To run the App, you can either connect a physical Android device or use an emulator to test your app.

When the App opens, press the "Record" button to start recording and press it again to stop.


That's it for today's tutorial guys. I really hope you enjoyed this blog. Thank you so much for taking the time to read. As always, if you're having any trouble writing the code or running the App, please let me know.

It's for beginners so when you face some errors or if the App crashes, don't be too hard on yourself. Just share your concerns in the comments section amd I'll try to be of help. As you keep learning and developing more basic Apps, you will soon become a master.

Have a lovely day and catch you next time on StemSocial. Goodbye 👨‍💻


You Can Follow Me @skyehi For More Like This And Others



0
0
0.000
4 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Thanks for including @stemsocial as a beneficiary, which gives you stronger support. 
 

0
0
0.000
avatar

Congratulations!


You have obtained a vote from CHESS BROTHERS PROJECT

✅ Good job. Your post has been appreciated and has received support from CHESS BROTHERS ♔ 💪


♟ We invite you to use our hashtag #chessbrothers and learn more about us.

♟♟ You can also reach us on our Discord server and promote your posts there.

♟♟♟ Consider joining our curation trail so we work as a team and you get rewards automatically.

♞♟ Check out our @chessbrotherspro account to learn about the curation process carried out daily by our team.


🏅 If you want to earn profits with your HP delegation and support our project, we invite you to join the Master Investor plan. Here you can learn how to do it.


Kindly

The CHESS BROTHERS team

0
0
0.000
avatar

Creating an Android Sound Recorder App can be a rewarding venture for developers. To embark on this journey, one can leverage various programming tools and platforms, ensuring a user-friendly and feature-rich application. Stay updated on the latest developments, and consider exploring innovative features like those available on the live ten sports apk to set your Sound Recorder App apart in the competitive app landscape. Incorporating functionalities like real-time audio recording and playback enhances the app's utility. As you delve into the development process, explore resources and tools that facilitate a seamless experience.

Posted using STEMGeeks

0
0
0.000
avatar

Very interesting comment you seem to already have experience in it

0
0
0.000