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

অ্যান্ড্রয়েড অ্যাপে SpeechRecognizer API ব্যবহার করার পদ্ধতি

এই উদাহরণটি দেখায় কিভাবে অ্যান্ড্রয়েডে SpeechRecognizer API ব্যবহার করা যায়।

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

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

ধাপ ২: লেআউট XML ফাইল যোগ করুন

res/layout/activity_main.xml ফাইলে নিচের কোডটি যোগ করুন।

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16sp"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:textSize="16sp"
        android:textStyle="bold" />
</LinearLayout>

ধাপ ৩: MainActivity.java ফাইলে কোড যোগ করুন

src/MainActivity.java ফাইলে নিচের কোডটি যোগ করুন। RecognizerIntent.ACTION_RECOGNIZE_SPEECH ব্যবহার করে স্পিচ রিকগনিশন শুরু করা হয়েছে।

package app.com.sample;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.widget.TextView;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private final int REQUEST_SPEECH_RECOGNIZER = 3000;
    private TextView textView;
    private final String mQuestion = "Which company is the largest online retailer on the planet?";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        startSpeechRecognizer();
    }

    private void startSpeechRecognizer() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, mQuestion);
        startActivityForResult(intent, REQUEST_SPEECH_RECOGNIZER);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SPEECH_RECOGNIZER) {
            if (resultCode == RESULT_OK) {
                List results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                String mAnswer = results.get(0);
                if (mAnswer.toUpperCase().contains("AMAZON")) {
                    textView.setText(String.format("\n\nQuestion: %s\n\nYour answer is '%s' and it is correct!", mQuestion, mAnswer));
                } else {
                    textView.setText(String.format("\n\nQuestion: %s\n\nYour answer is '%s' and it is incorrect!", mQuestion, mAnswer));
                }
            }
        }
    }
}

ধাপ ৪: AndroidManifest.xml-এ রেকর্ড অডিও অনুমতি যোগ করুন

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
</manifest>

অ্যাপ্লিকেশন চালান

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

  1. এন্ড্রয়েডে ViewFlipper ব্যবহার করার পদ্ধতি

  2. অ্যান্ড্রয়েডে Snackbar ব্যবহার করার পদ্ধতি

  3. অ্যান্ড্রয়েডে CheckBox ব্যবহারের পদ্ধতি

  4. Android-এ NavigationView ব্যবহার করে নেভিগেশন ড্রয়ার তৈরির পদ্ধতি