অ্যান্ড্রয়েডে TextToSpeech বাস্তবায়ন গাইড
এই টিউটোরিয়ালটি দেখাবে কিভাবে একটি অ্যান্ড্রয়েড অ্যাপে টেক্সট-টু-স্পিচ (TextToSpeech) ফিচার যোগ করা যায়। ব্যবহারকারী টেক্সট লিখে, পিচ (Pitch) এবং স্পিড (Speed) এডজাস্ট করে বক্তৃতা শোনতে পারবেন।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio খুলুন, File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট XML কনফিগার করুন
res/layout/activity_main.xml ফাইলে নিচের কোড যোগ করুন। এখানে একটি EditText, দুটি SeekBar (পিচ এবং স্পিডের জন্য) এবং একটি বাটন রয়েছে।
<?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:gravity="center"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:hint="টেক্সট লিখুন" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="পিচ (Pitch)"
android:textSize="16sp" />
<SeekBar
android:id="@+id/seekBarPitch"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:progress="50" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="গতি (Speed)"
android:textSize="16sp" />
<SeekBar
android:id="@+id/seekBarSpeed"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:progress="50" />
<Button
android:id="@+id/btnSpeak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:enabled="false"
android:text="বোলুন!" />
</LinearLayout>
ধাপ ৩: MainActivity.java এ লজিক লিখুন
src/MainActivity.java ফাইলে নিচের কোড যোগ করুন। TextToSpeech ইনিশিয়ালাইজেশন, ভাষা সেটিং, এবং বোলে آنয়시키는 লজিক এখানে রয়েছে।
import android.speech.tts.TextToSpeech;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private TextToSpeech textToSpeech;
private EditText editText;
private SeekBar seekBarPitch, seekBarSpeed;
private Button buttonSpeak;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonSpeak = findViewById(R.id.btnSpeak);
editText = findViewById(R.id.editText);
seekBarPitch = findViewById(R.id.seekBarPitch);
seekBarSpeed = findViewById(R.id.seekBarSpeed);
textToSpeech = new TextToSpeech(this, status -> {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.ENGLISH);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TextToSpeech", "ভাষা সমর্থিত নয়");
} else {
buttonSpeak.setEnabled(true);
}
} else {
Log.e("TextToSpeech", "ইনিশিয়ালাইজেশন ব্যর্থ");
}
});
buttonSpeak.setOnClickListener(v -> speak());
}
private void speak() {
String text = editText.getText().toString();
float pitch = seekBarPitch.getProgress() / 50f;
float speed = seekBarSpeed.getProgress() / 50f;
if (pitch < 0.1f) pitch = 0.1f;
if (speed < 0.1f) speed = 0.1f;
textToSpeech.setPitch(pitch);
textToSpeech.setSpeechRate(speed);
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
}
@Override
protected void onDestroy() {
if (textToSpeech != null) {
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onDestroy();
}
}
ধাপ ৪: ম্যানিফেস্ট আপডেট করুন
AndroidManifest.xml ফাইলটি সাধারণ ডিফল্ট কনফিগারেশন 그대로 রাখুন। কোনো অতিরিক্ত পারমিশনের প্রয়োজন নেই।
অ্যাপ চালানোর পদ্ধতি
অ্যান্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত করুন। Android Studio-তে Run আইকনে ক্লিক করে ডিভাইস সিলেক্ট করুন। অ্যাপ লঞ্চ হলে টেক্সট বক্সে লিখুন, পিচ ও স্পিড সেট করুন, তারপর "বোলুন!" বাটনে ট্যাপ করুন।