How To Build A Song Detector Android App - Hive Programmers

avatar

Greetings to my favorite science community online, StemSocial.

It's @skyehi and I'm super excited to be back to continue my series on Android App Development Tutorials for Beginners.

Today's blog or episode is going to be a really special one because I don't recall working on any music related Apps in this series.

Polish_20240106_164623116.jpgOriginal Image Source by Soulful Pizza from Pexels

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

There are quite a handful of Apps out there that have the ability to listen to any song playing around just by the user pressing a record button and then the App detects what music is playing.

The App is able to detect the song and display the song title, the singer, album and even links to watch on different platforms the song had been published on.

These kinds of Apps are really helpful for when you hear a new song you're not too familiar with and you want to find the song.

You simply press a record or listen to song button in the App and the App can detect the song and also give details about it, including lyrics.

That's what we're going to be working on in today's episode and I hope my friends @stickupcurator or @stickupboys will appreciate this particular episode 🥰

Without wasting anymore time, let's get started with our work.

Just a reminder though, this particular App is more like a Model App and you can add more features to it. I create basic and model Apps because we're still at the beginner's level of thr series. As we advance, we'll have more in depth look at complex codes.

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Main Features Of Our Song Detector

Now guys to build a song detector App like "Shazam", the primary features of the App should include audio recording, analysis, and song recognition.

When it comes to Audio recording, we have already built an Audio recorder in this series and it really doesn't require that much work.

However when building the Audio Recorder, please ensure that you request the RECORD_AUDIO permission in Android Manifest.

After recording the audio that is playing, the app would make an analysis of the audio and then use an Audio recognition API to detect the song and display it in the App.

So we would most definitely need to choose a Recognition API for our App.

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Choosing a Recognition API

Now guys to make things simple, I have selected a recognition API which you can use. It's called ACRCloud SDK.

To integrate this API into your Android Studio Project, you have to go to their website and sign up for an account and obtain your Access Key, Access Secret, and Project ID.

Here's the link to their website guys

ACRCloud Main Website

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Setting Up Android Studio

Well if you follow most of my blogs regarding this series, I usually start with setting up Android Studio but I knew how important it was to talk about the primary features of the App which included the Audio Recognition API which was why I had to start with that first.

For the sake of newcomers to this series and to Android App development, if you want to be able to go through with developing Android Apps you have to ensure that you have two main softwares installed.

  • Android Studio IDE which is the platform we'll be building our Android Apps on
  • Java Development Kit, JDK which is required for your PC to be able to execute Java codes.

If you have these softwares successfully installed on you PC then you're good to go.


We'll start by creating a new Android Studio Project for our Song Detector App. To create a new project, simply open Android Studio and click on "Create a New Android Project".

You will be given a number of project templates to choose from. I'll recommend selecting the Empty Activity template for simplicity sake.

You can also set the App name and package name of your App.

Please ensure that Java is the selected programming language and not Kotlin.

After you're through with this and all other configurations based on your preferences, click the "Finish" button and Android Studio will prepare your new Android Project.

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

First Phase: Implement Audio Recording

So guys, like I said at the beginning for our Song Detector App, we need to implement an Audio recording feature and then integration a detection API.

Thankfully Android Studio comes with a built-in class or API for Audio recoding feature. It's called MediaRecorder.

To integrate it, you would need to first add the necessary permission in the AndroidManifest.xml file.

This Permission code is needed in our App code so the App can have access to the Audio recording service or feature of any Android Device.

Here's how the Audio recording permission code should look like.

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

Of course you would have to create a button that will initiate the audio recording, then our recognition API will pick the data we got from recording audio and enable us to detect what song it is.

So I'll share a really brief code on the Audio Recording activity because we have already built an entire Audio recorder in this series and it's not time for more codes that would overwhelm the beginner's.

Here's how your Recording Activity Java file should look like

public class RecordActivity extends AppCompatActivity {
    private MediaRecorder mediaRecorder;

    // ... other code ...

    private void startRecording() {
        mediaRecorder = new MediaRecorder();
        // ... configure mediaRecorder ...

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

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

Second Phase: Audio Analysis

Now the second step to building our Song Detector App is to include some libraries that would help us with audio analysis.

The reason we want to Implement audio analysis is to enable us extract features. We can get the exact data of any audio, compare it with the database from that recognition API and be able to quickly detect the song playing.

You can use libraries like TarsosDSP for pitch analysis.

Here's how the Audio analysis code would look like guys

PitchDetectionHandler pitchDetectionHandler = (result, timeStamp) -> {
    final float pitchInHz = result.getPitch();
    // Process pitch information...
};

AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050, 1024, 0);
dispatcher.addAudioProcessor(new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, pitchDetectionHandler));
new Thread(dispatcher, "Audio Dispatcher").start();

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Third Phase: Integrate Song Recognition API

It's now time for our third and final phase, which involves implementing the song recognition API.

We can now integrate the ACRCloud SDK. To integrate this API, we would have to add its dependencies in our app's build.gradle file.

So guys locate the gradle file and include the dependency. Now you could either copy mine or ideally, go to the website and head over to the implementation guide to see the latest version of their dependency

Here's how the dependency code should look like

implementation 'com.acrcloud.android:acrcloud-android-sdk:1.1.0'

After you're done adding the code, click on the "Sync" button and Android Studio will download all the necessary library files to integrate ACRCloud to your project.

When you're through with including the dependency, the next thing to do is to initialize the ACRCloud client in your code.

Here's how the initialization code should look like

ACRCloudClient mClient = new ACRCloudClient();
mClient.initWithAccessKey("YOUR_ACCESS_KEY", "YOUR_ACCESS_SECRET", "YOUR_PROJECT_ID", "YOUR_HOST");

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

User Interface Design

Now guys normally, I would work on User interface first but who ever paid attention to yesterday's post probably knows why I did this.

Some developers like to work on backend before User Interface and I just wanted to try that out in this episode 😁.

I hope it didn't confuse anyone though.

We have to design our app's UI using XML layouts. You would have to create buttons for recording and displaying recognition results.

So for a simple and basic App like ours, we would need two main elements. A Button element for starting the recording or listening to the song and a TextView element that would display the details of the song which could include title and singer or author's name.

Here's how the design code should look like - You are free to improve on this and tailor it to your desire.

<Button
    android:id="@+id/btnRecord"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Record" />

<TextView
    android:id="@+id/tvResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Recognition Result" />

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

There are a few steps to go through to improve on the App though guys. I will just list them out for you briefly.

  • Testing

Test the app thoroughly, ensuring accurate song recognition and a smooth user experience.

  • Permissions and Security

Handle necessary permissions for audio recording and implement secure coding practices.

  • Optimization

Optimize the app's performance, considering factors like response time and resource usage.

  • User Feedback

Implement user feedback mechanisms, such as notifications or confirmation dialogs.

  • Documentation

Document your code and app functionalities for reference or collaboration. This is a very important part of what helps Programmers create good updates of their Apps.

  • Publish

Publish your app on the Google Play Store after successful testing.

&&&&&

Thank you so much for taking the time to read today's blog guys. I gotta admit, today's episode is a bit different from the usual style and I hope you like this too.

As always if you're having troubles with installing the required softwares for programming, writing the codes, downloading the libraries or running the App, please let me know in the comments.

Have a Great Day, a Great Year and Catch you Next Time on StemSocial. Goodbye 👨‍💻


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



0
0
0.000
1 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