A Quick Introduction about How to Implement Sound Detection
For some apps, it’s necessary to have a function called sound detection that can recognize sounds like knocks on the door, rings of the doorbell, and car horns. Developing such a function can be costly for small- and medium-sized developers, so what should they do in this situation?
There’s no need to worry about if you have the sound detection service in HUAWEI ML Kit. Integrating its SDK into your app is simple, and you can equip it with the sound detection function that can work well even when the device does not connect to the network.
Introduction to Sound Detection in HUAWEI ML Kit
This service can detect sound events online by real-time recording. 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, cat meowing, dog barking, running water (such as from taps, streams, and ocean waves), car horns, doorbells, knocking on doors, fire alarms (including smoke alarms), and sirens (such as those from fire trucks, ambulances, police cars, and air defenses).
Preparations
Configuring the Development Environment
- Create an app in AppGallery Connect.
For details, see Getting Started with Android.
2. Enable ML Kit.
Click here to get more information.
3. After the app is created, an agconnect-services.json file will be automatically generated. Download it and copy it to the root directory of your project.
4. Configure the Huawei Maven repository address.
To learn more, click here.
5. Integrate the sound detection SDK.
It is recommended to integrate the SDK in full SDK mode. Add build dependencies for the SDK in the app-level build.gradle file.
6. Add the AppGallery Connect plugin configuration as needed using either of the following methods:
Method 1: Add the following information under the declaration in the file header:
Method 2: Add the plugin configuration in the plugins block:
Automatically update the machine learning model.
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.
For details, go to Integrating the Sound Detection SDK.
Development Procedure
- Obtain the microphone permission. If the app does not have this permission, error 12203 will be reported.
(Mandatory) Apply for the static permission.
2. Create an MLSoundDector object.
3. Create a sound detection result callback to obtain the detection result and pass the callback to the sound detection instance.
// Create a sound detection result callback to obtain the detection result and pass the callback to the sound detection instance.
Note: The code above prints the type of the detected sound as an integer. In the actual situation, you can convert the integer into a data type that users can understand.
Definition for the types of detected sounds:
4. Start and stop sound detection.
5. Call destroy() to release resources when the sound detection page is closed.
Testing the App
- Using the knock as an example, the output result of sound detection is expected to be 10.
- Tap Start detecting and simulate a knock on the door. If you get logs as follows in Android Studio’s console, they indicates that the integration of the sound detection SDK is successful.
More Information
- Sound detection belongs to one of the six capability categories of ML Kit, which are related to text, language/voice, image, face/body, natural language processing, and custom model.
- Sound detection is a service in the language/voice-related category.
- Interested in other categories? Feel free to have a look at the HUAWEI ML Kit document.
To learn more, please visit:
>> HUAWEI Developers official website
>> Reddit to join developer discussions
>> GitHub or Gitee to download the demo and sample code
>> Stack Overflow to solve integration problems
Follow our official account for the latest HMS Core-related news and updates.