DEV Community

Cover image for 🎙️ Introducing react-native-voice2text: Seamless Voice-to-Text for React Native Android Apps 📱
Gokul Krishna. S
Gokul Krishna. S

Posted on

🎙️ Introducing react-native-voice2text: Seamless Voice-to-Text for React Native Android Apps 📱

Voice interaction is revolutionizing mobile app development. Whether it’s enabling hands-free control 🤚, enhancing accessibility ♿, or speeding up input ⏩, speech-to-text functionality is a must-have for modern apps. If you’re a React Native developer building for Android, I’m thrilled to introduce react-native-voice2text — a lightweight, user-friendly module that leverages Android’s native speech recognition APIs to convert voice to text in real-time. 🗣️➡️📝

Designed for simplicity and reliability, react-native-voice2text makes it easy to add voice features to your React Native apps. Let’s explore why this package is a game-changer and how you can get started!

Why Choose react-native-voice2text? ✅

Here’s what sets this library apart:

  • 🎯 Seamless Integration: Built for React Native 0.70+, it supports autolinking for a hassle-free setup. ⚙️
  • 🎤 Real-Time Recognition: Streams speech to text instantly, perfect for voice commands and hands-free inputs. 🔄
  • 🔐 Built-in Permission Handling: Manages microphone permissions effortlessly, no extra configuration needed. 🔒
  • 🚫 Robust Error Handling: Provides clear error callbacks for a smooth user experience. 🛠️
  • 🤖 Powered by Android Native APIs: Uses Android’s native speech services for high accuracy and performance. 📡
  • 🚧 iOS Support Coming Soon!: iOS compatibility is in the works, so stay tuned. 🍏

Whether you’re creating a chat app 💬, a hands-free utility 🤖, or accessibility tools ♿, react-native-voice2text is your go-to solution for voice-to-text with minimal effort.

🚀 Getting Started

Ready to add voice-to-text to your React Native app? Follow these simple steps to integrate react-native-voice2text and start converting speech to text in no time!

Step 1: Install the Package

Install the package using npm or Yarn:

npm install react-native-voice2text
# or
yarn add react-native-voice2text
Enter fullscreen mode Exit fullscreen mode

Step 2: Update Android Permissions

To enable microphone access, add the following permissions to your AndroidManifest.xml (located at android/app/src/main/AndroidManifest.xml):

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
Enter fullscreen mode Exit fullscreen mode

Step 3: Implement Voice Input

Here’s a complete example of using react-native-voice2text in a React Native app:

import React, { useEffect } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
import Voice2Text from 'react-native-voice2text';

const App = () => {
  const startListening = async () => {
    try {
      const granted = await Voice2Text.checkPermissions();
      if (granted) {
        Voice2Text.startListening('en-US'); // Start listening in English (US)
      } else {
        console.warn('Microphone permission denied 🚫🎤');
      }
    } catch (error) {
      console.error('Error checking permissions:', error);
    }
  };

  useEffect(() => {
    // Set up event listeners for results and errors
    Voice2Text.onResults(({ text }) => {
      console.log('Recognized text:', text);
    });

    Voice2Text.onError(({ message }) => {
      console.error('Error:', message);
    });

    // Clean up listeners on component unmount
    return () => {
      Voice2Text.removeAllListeners();
    };
  }, []);

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Voice-to-Text Demo</Text>
      <TouchableOpacity style={styles.button} onPress={startListening}>
        <Text style={styles.buttonText}>Start Voice Input 🎤</Text>
      </TouchableOpacity>
    </View>
  );
};

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center', padding: 20 },
  title: { fontSize: 24, fontWeight: 'bold', marginBottom: 20 },
  button: { backgroundColor: '#007AFF', padding: 15, borderRadius: 10 },
  buttonText: { color: '#fff', fontSize: 18 },
});

export default App;
Enter fullscreen mode Exit fullscreen mode

What’s Happening Here?

  • checkPermissions: Verifies microphone access.
  • startListening: Initiates speech recognition, specifying the language (e.g., 'en-US').
  • onResults: Receives the recognized text and logs it to the console.
  • onError: Handles any errors during recognition.
  • The UI includes a TouchableOpacity button styled for a native mobile experience.

Step 4: Run Your App

Rebuild and run your app on an Android device or emulator:

npx react-native run-android
Enter fullscreen mode Exit fullscreen mode

Speak into your device’s microphone, and the recognized text will appear in your console! 🎤 Note: Ensure your device or emulator has a working microphone for testing.

💡 Why Add Voice-to-Text to Your App?

Voice capabilities can elevate your app’s usability and inclusivity. Here are some compelling use cases for react-native-voice2text:

  • Accessibility: Empower users with visual or motor impairments to interact via voice. ♿
  • Hands-Free Operation: Enable voice commands for users on the move, like drivers or multitaskers. 🚗
  • Faster Input: Allow users to dictate messages or notes, saving time. ⏩
  • Innovative Features: Create voice-driven search, real-time transcription, or other unique functionalities. 🔍

By integrating react-native-voice2text, you’re making your app more accessible, intuitive, and future-proof.

☕ Support the Project

I’m Gokul, the creator and maintainer of react-native-voice2text. Building open-source libraries takes time and passion, and your support keeps me going! If you find this package helpful, please consider buying me a coffee to fuel my coding sessions. ☕❤️

You can also contribute by:

  • ⭐ Starring the project on GitHub.
  • 🐛 Reporting bugs or suggesting features via GitHub issues.
  • 🛠️ Submitting pull requests to enhance the codebase.

🚀 Try It Out Today!

Ready to bring voice-to-text to your React Native app? Dive into the full documentation and get started with react-native-voice2text on npm: npmjs.com/package/react-native-voice2text.

Have questions or ideas? Drop a comment below or reach out via the GitHub repository. I’d love to hear how you’re using react-native-voice2text in your projects! 💬

Let’s make mobile apps more interactive and inclusive with the power of voice. Happy coding! 🚀


Top comments (0)