Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड ऐप में TextToSpeech कैसे बनाएं?

एंड्रॉइड ऐप में टेक्स्ट-टू-स्पीच (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 आइकन पर क्लिक करें और अपना डिवाइस चुनें। ऐप खुलने पर टेक्स्ट दर्ज करें, पिच और गति सेट करें, फिर "बोलो!" बटन दबाएं।

  1. Android ऐप में मल्टी-लेवल ExpandableListView कैसे बनाएं? Android ऐप में मल्टी-लेवल ExpandableListView कैसे बनाएं?

    परिचय यह ट्यूटोरियल दर्शाता है कि Android में ExpandableListView का उपयोग करके दो-स्तरीय (ग्रुप → चाइल्ड) सूची कैसे बनाएं। हम खेल श्रेणियों (क्रिकेट, फुटबॉल, टेनिस) और उनके खिलाड़ियों का उदाहरण बनाएंगे। आवश्यक चरण चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project से नया प्रोजेक्ट बन

  1. Android ऐप में XML फ़ाइल से एनिमेशन कैसे बनाएं? Android ऐप में XML फ़ाइल से एनिमेशन कैसे बनाएं?

    परिचय यह ट्यूटोरियल दिखाता है कि Android में XML फाइलों से एनिमेशन कैसे परिभाषित करें और उन्हें व्यूज़ पर लागू करें। हम तीन प्रकार की एनिमेशन बनाएंगे: फेड-इन (अल्फा), जूम (स्केल), और ब्लिंक (रिपीटिंग अल्फा)। आवश्यक चरण चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project से नया प्रोजेक

  1. किसी भी Android ऐप के लिए होमस्क्रीन शॉर्टकट कैसे बनाएं किसी भी Android ऐप के लिए होमस्क्रीन शॉर्टकट कैसे बनाएं

    <हम में से अधिकांश जानते हैं कि हमारे फोन पर पहले से मौजूद ऐप्स के लिए होमस्क्रीन शॉर्टकट कैसे जोड़ें - बस ऐप ड्रॉअर खोलें, ऐप आइकन पर तब तक दबाए रखें जब तक आप उसे पकड़ न लें, फिर इसे अपनी पसंद की होमस्क्रीन पर खींचें। लेकिन हो सकता है आप विशिष्ट फ़ोल्डर्स, वेब पेज, या अपने संपूर्ण बुकमार्क संग्रह