Introduction In this article, I will show you a basic Android application that can listen to the user's voice and convert it into text data. In addition, this application can also perform text analysis and then execute corresponding commands to implement data storage and user response functions. Note: The source code for this article can be downloaded from https://github.com/sitepoint-editors/SpeechApplication. The program snapshot is as follows:
Create an application Open Android Studio and create a new project. Select the minimum version of Android API 18 and add an empty Activity. This is also the only Activity in this project. To achieve full-screen display of the view, open the configuration file AndroidManifest.xml and set it as follows:
This configuration will hide the ActionBar in our current activity. At this point, you already have a full-screen white background layout view with only one TextView control. To make some improvements, you can add a gradient shape to the RelativeLayout. Next, right-click the drawable folder and select New->Drawable resource file. Name this resource file background and replace the original content with the following code:
In fact, you can modify the color and angle as you like. Note: The ImageButton control in the layout uses an image from https://design.google.com/icons/#ic_mic_none. You can download it and add it as a resource. Next, update the code in the activity_main.xml file:
Add speaking function Now that the user interface is complete, it is time to write the Java code inside MainActivity. First, declare a variable TextToSpeech above the onCreate method:
Then, add the following code to the onCreate method:
The above code will start the TextToSpeech service in the system. The speak() method uses a String type parameter, which is the text you want your Android device to read out. Next, create this method and add the following code:
The Build.VERSION check is used in the above code because calls of the form tts.speak(param,param,param) are deprecated for Android API 5.1. Create another method after the speak() method to stop the TextToSpeech service when the user closes the program:
At this point, once you start the program, it can say "Hello". The next step is to make the program listen. Add listening function To enable the program to listen, you need to use the microphone button. To do this, add the following code to the onCreate method:
When the ImageButton control is clicked, the following function will be called:
This method will start the listening Activity, which will display a dialog box with a text prompt. The language used for the speech is provided by the device through the Locale.getDefault() method. The StartActivityForResult(i, 100) method waits for the current activity to return a result. 100 is just a random code attached to the started activity, but it can be any number that suits your needs. When the result is returned from the started activity, it contains this code and uses this code to distinguish multiple results from each other. To capture the results from the launched activity, you need to add the following override method:
This method captures each result from the activity and uses the requestCode to process the language identifier result data. If requestCode equals 100, requestCode equals OK and the data from this result is not null. You will get the result string from res.get(0). Next, create a new method, recognition, which will take a parameter of type String:
So far, when the user clicks the microphone button, the program can listen to the sound and convert the user's language into text data. The final result will be printed out through the Error log. Add learning function To make the program more interesting, in this section you will enable the application to learn some simple things, such as your name. To achieve this, you need to use the local storage feature. First, add the following code to the onCreate method:
Then, add the following code to the onCreate method:
First, you need to make the application ask questions, so you need to change speak("Hello") to speak("What is your name?"). Here you can use a simple logic; so when someone asks "What is your name?" and the answer is "My name is Dori", then take the name from the answer. A simple way to do this is to split the answer into strings separated by spaces and get the value of the last index. So, we need to update the code in the recognition method as follows:
The recognize method uses all the results from the user's speech. Since the utterances may be different, you can use certain words they may contain to distinguish them. For example, the code in this method could be:
But it's still a simple interaction with the app. You can have it learn your age, or give it a name. In the same method, you can try these simple conditions:
The app can tell you the time:
summary There are more examples in the GitHub project I created (https://github.com/sitepoint-editors/SpeechApplication), so you can fully experiment and develop your own real Android assistant application. In the end, I hope you enjoyed this tutorial and that you were able to have a truly useful conversation with your phone. |
We have to watch our competitors taking orders wh...
The X-59 is a unique experimental aircraft. It is...
This is a methodology. Yes, there is no actual ca...
It's August in a blink of an eye. In just ove...
Fifteen Japanese companies , including Toyota, Ho...
Is nuclear waste water harmful? This question its...
If you want to advertise on your own cross-border...
Speaking of information flow, we have to mention ...
Recently, overseas media exposed a group of spy p...
In western Sichuan, China There is a special litt...
Is this dish edible? Boil water It’s time to eat ...
Author: Hatton (student of the Science Voice Know...
What is a behavioral model? In fact, it is a habi...
The refrigerator is a great invention. By throwin...
Before we knew it, someone posted a message on We...