एंड्रॉइड ऐप में टेक्स्ट-टू-स्पीच (TTS) लागू करना
यह उदाहरण दिखाता है कि एंड्रॉइड ऐप में TextToSpeech फीचर कैसे जोड़ा जाता है, जिससे ऐप लिखे गए टेक्स्ट को आवाज़ में बदल सके।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project चुनें और आवश्यक विवरण भरकर नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट XML में UI जोड़ें
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: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>
चरण 3: MainActivity में लॉजिक लिखें
src/MainActivity.java में निम्न कोड जोड़ें:
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);
// TTS इंजन इनिशियलाइज़ करें
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("TTS", "भाषा समर्थित नहीं है");
} else {
buttonSpeak.setEnabled(true);
}
} else {
Log.e("TTS", "इनिशियलाइज़ेशन विफल");
}
});
buttonSpeak.setOnClickListener(v -> speak());
}
private void speak() {
String text = editText.getText().toString().trim();
if (text.isEmpty()) return;
float pitch = seekBarPitch.getProgress() / 50f;
float speed = seekBarSpeed.getProgress() / 50f;
pitch = Math.max(pitch, 0.1f);
speed = Math.max(speed, 0.1f);
textToSpeech.setPitch(pitch);
textToSpeech.setSpeechRate(speed);
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, "utteranceId");
}
@Override
protected void onDestroy() {
if (textToSpeech != null) {
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onDestroy();
}
}
चरण 4: Manifest में अनुमति जोड़ें
AndroidManifest.xml में इंटरनेट अनुमति जोड़ें (यदि ऑनलाइन TTS उपयोग कर रहे हैं):
<?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>
</manifest>
ऐप चलाएं
अपने एंड्रॉइड डिवाइस को USB से कनेक्ट करें, Android Studio में Run आइकन पर क्लिक करें और अपना डिवाइस चुनें। ऐप खुलने पर टेक्स्ट दर्ज करें, पिच और गति सेट करें, फिर "बोलो!" बटन दबाएं।