কম্পিউটার টিউটোরিয়াল

অ্যান্ড্রয়েডে Speech to Text সংহত করার পদ্ধতি


অ্যান্ড্রয়েড RecognizerIntent.ACTION_RECOGNIZE_SPEECH ব্যবহার করে গুগলের ইনবিল্ট টেক্স্ট টু স্পিচ API সমর্থন করে। এই উদাহরণটি অ্যান্ড্রয়েড স্পিচ টু টেক্সট সংহত করার পদ্ধতি প্রদর্শন করে।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project-এ যান এবং একটি নতুন প্রজেক্ট তৈরি করতে সমস্ত প্রয়োজনীয় বিবরণ পূরণ করুন।

ধাপ ২: 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>

উপরোক্ত কোডে আমরা একটি TextView এবং ImageView তৈরি করেছি। ব্যবহারকারী ImageView-এ ক্লিক করলে, এটি গুগল স্পিচ টু টেক্সট API কল করবে এবং টেক্সট TextView-এ যোগ করবে।

ধাপ ৩: 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, "Need to speak");
                try {
                    startActivityForResult(intent, REQ_CODE);
                } catch (ActivityNotFoundException a) {
                    Toast.makeText(getApplicationContext(), "Sorry your device not supported", 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;
            }
        }
    }
}

উপরোক্ত কোডে ব্যবহারকারী ImageView-এ ক্লিক করলে, এটি নিচের মত ইন্টেন্ট কল করবে:

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, "Need to speak");
try {
    startActivityForResult(intent, REQ_CODE);
} catch (ActivityNotFoundException a) {
    Toast.makeText(getApplicationContext(), "Sorry your device not supported", Toast.LENGTH_SHORT).show();
}

উপরোক্ত কোডে, আমরা গুগল 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-এ থেকে শূন্য পজিশন নেওয়া হচ্ছে এবং TextView-এ অ্যাপেন্ড করছি।

আপনার অ্যাপ্লিকেশন চালানোর চেষ্টা করুন। আমি ধরে নিচ্ছি আপনার আসল Android মোবাইল ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে। Android Studio থেকে অ্যাপ চালাতে, আপনার প্রজেক্টের একটি এক্টিভিটি ফাইল খুলুন এবং টুলবার থেকে Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস নির্বাচন করুন এবং তারপর আপনার মোবাইল ডিভাইস চেক করুন যা আপনার ডিফল্ট স্ক্রিন প্রদর্শন করবে।

অ্যান্ড্রয়েডে Speech to Text সংহত করার পদ্ধতি

ব্যবহারকারী মাইক বাটনে ক্লিক করলে, এটি গুগল API কল করবে:

অ্যান্ড্রয়েডে Speech to Text সংহত করার পদ্ধতি

এখন আমরা ইনপুট দিয়েছি "Hey GOOGLE"। এটি নিচের ফলাফল অ্যাপেন্ড করবে:

অ্যান্ড্রয়েডে Speech to Text সংহত করার পদ্ধতি


  1. Android-এ TextView-এ টেক্সট জাস্টিফাই (Justify) করা

  2. অ্যান্ড্রয়েডে টেক্সট ফরওয়ার্ড করার পদ্ধতি

  3. অ্যান্ড্রয়েডের টেক্সট-টু-স্পিচ ফিচার ব্যবহার করুন: ধাপে ধাপে গাইড

  4. অ্যান্ড্রয়েডে গুগল টেক্স্ট-টু-স্পিচ ভয়েস সহজে পরিবর্তন করুন – ধাপে ধাপে টিউটোরিয়াল