Speech to Text Conversion in React Native – Voice Recognition

This is an example to show how to do Speech to Text Conversion in React Native – Voice Recognition. This is a very demanding feature from many of the customers after the success of intelligent voice assistances like Google Home and Amazon Alexa. To make your app different from another app you can implement voice recognition feature while making search filters. In the current scenario, many applications have voice search with text input search where they provide either search by text input or can search by voice.

Here we are going to see how can we convert voice to text and can get the texts as a result with the help of voice recognition.

For Voice Recognition in React Native

For the voice recognition in React Native or we can say that for the speech to text conversion we are going to use Voice component provided by react-native-voice library which has a number of events which can be used to start or stop the voice recognition and to get the status and of voice recognition.

When we initialize the screen we set some event callbacks in the constructor which looks like the code snippet given below. You can see we are providing a function for the SpeechStart or for the Speech End and so on. So this are the callbacks basically which will be called automatically when the event happens.

So these were the callback events to get the status of voice recognition. Now let’s see how to start, stop, cancel and destroy voice recognition process.

Start Voice Recognition

Stop Voice Recognition

Cancel Voice Recognition

Destroy the session of Voice Recognition

I think this is enough to know about the library, now let’s move towards the code. In this example, we are going to make a screen with a mike icon which will be the clickable button for us and after clicking onto that we will start voice recognition. In the whole process, we will get the status of everything in the functions we have set for the callback. We can also stop the speech to text conversion by using stop,  cancel and destroy button.

One more point I want to add in this is we will get two types of results while and after voice recognition which are:

  1. Result: It comes when Speech Recognizer finishes recognization.
  2. Partial Results: It comes during the computation of results, so this is kind of words which voice recognizer recognize before the final result. Partial results can be many for a single recognition as it is an intermidite result.

Now let’s get started with the example and see how to convert speech to text.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use Voice component we have to install react-native-voice dependency. To install the dependency open the terminal and jump into your project

Now install the dependency

CocoaPods Installation

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the libraries but need to install pods. In this example, we need to install the pods for react-native-voice.

Please use the following command to install CocoaPods

Permission to use microphone and speech recognition for IOS

Please follow the below steps to add the permission in the iOS project to use the microphone and speech recognition.

Open the project SpeechToTextExample -> ios -> SpeechToTextExample.xcworkspace in Xcode.

1. After opening the project in Xcode click on the project from the left sidebar and you will see multiple options in the right workspace.

2. Select info tab which is info.plist

3. Now add two permissions key “Privacy-Microphone Usage Description” and “Privacy-Speech Recognition Usage Description”. You can also set the value which will be visible when permission dialog pops up. Here are the screenshots below for the reference.

Code to Convert Speech to Text

Now Open App.js in any code editor and replace the code with the following code.

App.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Android

Image   Image   Image   Image

iOS

Image   Image   Image

This is how you can do speech to text conversion / Voice Recognition in React Native. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.