Android Speech to Text इंटीग्रेशन: वॉयस इनपुट ट्यूटोरियल
Android, RecognizerIntent.ACTION_RECOGNIZE_SPEECH का उपयोग करके Google के इनबिल्ट टेक्स्ट टू स्पीक API का समर्थन करता है। इस उदाहरण में दर्शाया गया है कि Android Speech to Text कैसे इंटीग्रेट करें।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2 - activity_main.xml लेआउट
res/layout/activity_main.xml में निम्न कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:orientation="vertical"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:src="@android:drawable/ic_btn_speak_now"/>
</LinearLayout>
</RelativeLayout>
उपरोक्त कोड में हमने एक टेक्स्ट व्यू और इमेज व्यू बनाया है। जब उपयोगकर्ता इमेज व्यू पर क्लिक करता है, तो यह Google Speech to Text API को कॉल करेगा और टेक्स्ट को टेक्स्ट व्यू में जोड़ेगा।
चरण 3 - MainActivity.java
src/MainActivity.java में निम्न कोड जोड़ें:
package com.example.andy.myapplication;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private final int REQ_CODE = 100;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
ImageView speak = findViewById(R.id.speak);
speak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "बोलने की आवश्यकता है");
try {
startActivityForResult(intent, REQ_CODE);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(), "क्षमा करें आपका डिवाइस समर्थित नहीं है", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE: {
if (resultCode == RESULT_OK && data != null) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
textView.setText(result.get(0));
}
break;
}
}
}
}
उपरोक्त कोड में जब उपयोगकर्ता इमेजव्यू पर क्लिक करता है, तो यह इंटेंट को कॉल करेगा:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "बोलने की आवश्यकता है");
try {
startActivityForResult(intent, REQ_CODE);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(), "क्षमा करें आपका डिवाइस समर्थित नहीं है", Toast.LENGTH_SHORT).show();
}
उपरोक्त कोड में, हम Google API को कॉल कर रहे हैं और onActivityResult() पर परिणाम प्राप्त करेंगे:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE: {
if (resultCode == RESULT_OK && data != null) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
textView.setText(result.get(0));
}
break;
}
}
}
उपरोक्त कोड में, हम परिणाम को ArrayList के रूप में प्राप्त करेंगे इसलिए हम ArrayList से शून्य स्थिति ले रहे हैं और टेक्स्ट व्यू में जोड़ रहे हैं।
आइए अपने एप्लिकेशन को चलाने का प्रयास करें। मान लें कि आपने अपने वास्तविक Android मोबाइल डिवाइस को कंप्यूटर से जोड़ा है। Android Studio से ऐप चलाने के लिए, अपनी प्रोजेक्ट की एक्टिविटी फ़ाइलों में से एक खोलें और टूलबार से Run आइकन पर क्लिक करें। विकल्प के रूप में अपना मोबाइल डिवाइस चुनें और फिर अपने मोबाइल डिवाइस की जांच करें जो आपकी डिफ़ॉल्ट स्क्रीन प्रदर्शित करेगा।

जब उपयोगकर्ता माइक बटन पर क्लिक करता है, तो यह नीचे दिखाए अनुसार Google API को कॉल करेगा:

अब हमने इनपुट के रूप में "Hey GOOGLE" दिया है। यह नीचे दिखाए अनुसार परिणाम जोड़ देगा:
