Detect Environment Sounds With HMS ML Kit Sound Detection

Merve Nur TÜRK
Huawei Developers
Published in
3 min readDec 28, 2020

This story provides you to learn detecting sound around the environment using HMS ML Kit Sound Detector feature.

What is the Sound Detector ?

The sound detection service can detect sound events in online (real-time recording) mode. The detected sound events can help you perform subsequent actions. Currently, the following types of sound events are supported: laughter, child crying, snoring, sneezing, shouting, mew, barking, running water (such as water taps, streams, and ocean waves), car horns, doorbell, knocking, fire alarm sounds (such as fire alarm and smoke alarm), and other alarm sounds (such as fire truck alarm, ambulance alarm, police car alarm, and air defense alarm).

Before API development;

  • When you finish process of creating project, you need to get agconnect-services.json file for configurations from AppGallery Connect. Then, you have to add it into our application project level under the app folder.
  • After that, we need to add dependencies into project level gradle files.
  • Then, we need to add dependencies into app level gradle files.
  • Add the following statements to the AndroidManifest.xml file. After a user installs your app from HUAWEI AppGallery, the machine learning model is automatically updated to the user’s device.

Let’s learn sound detect sdk :)

This demo project aims to detect baby crying sounds around the user. The project has a main screen(Sound Detector Activity.java) that you can listen around.

Firstly app should check the permissions:

Then call the getRuntimePermissions() method at onCreate() override method. After checking permissions we create a sound detector as a global variable:

Then lets create sound detector and set the listener that if sound detector detects successfully or failed:

For this demo project, we focus for baby crying. You can look for other sound types and theirs ids from MLSoundDectConstants classes. After this implementation now, we can start the sound detector:

This method returns a boolen variable. Also you can write like:

if the boolean variable return true, sound detector start successfully. But if returns false, the detection fails to be started. The possible cause is that the microphone is occupied by the system or another app.

In addition to these, you may want to stop and destroy the sound detector on onStop() and onDestroy method:

In this article, we made a demo project using HMS ML Kit Sound Detection SDK and learn its usage. Hope to see awesome projects from you. Stay with code :)

References

--

--