ads_header

How To Create Speech To Text In Android Studio


How To Create Speech To Text In Android Studio



Preview



Hello guys, our discussion this time around android. I will share a little about how to create Speech To Text To Speech application.

So this application makes the voice we say changed in the form of text otherwise we can also change the text had become sound.

In making this program I use Android Studio IDE. Well just go to Android Studio and create a new project with Empty Project type. If you have made sure you have set up Microphone and Speaker images in the Drawable folder


Download Material Icons

If everything is done, you just delete content.xml in the layout folder because we only use activity_main.xml. Then open res → values → strings then enter the following code

<resources>
    <string name="app_name">Speech To Text To Speech</string>
    <string name="speech_prompt">Say Something</string>
    <string name="speech_not_supported">Sorry ! Your device doesn\'t support speech input</string>   
    <string name="tap_on_mic">Tap To Speak</string>
    <string name="tap_on_hear">Tap To Hear</string>
</resources>

then, open res → values → styles then enter the following code

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">   
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>


Now go to the layout folder and open activity_main.xml and enter the following code as the program's initial view

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#31244e"
    android:orientation="vertical"
    tools:context="id.ndiappink.speechtotext.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:id="@+id/txtSpeechInput"
        android:textAlignment="center"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:textColor="#FFFFFF"
        android:textSize="26dp"
        android:textStyle="normal"
        android:layout_height="wrap_content" />

    <ImageButton
        android:layout_width="wrap_content"
        android:id="@+id/btnvoicee"
        android:paddingTop="30dp"
        android:layout_centerHorizontal="true"
        android:background="@null"
        android:layout_below="@+id/txtSpeechInput"
        android:src="@drawable/voice"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:id="@+id/tapvoice"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/btnvoicee"
        android:layout_marginTop="10dp"
        android:text="@string/tap_on_hear"
        android:textColor="#FFFFFF"
        android:textSize="15dp"
        android:textStyle="normal"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="60dp"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_height="wrap_content">

        <ImageButton
            android:layout_width="wrap_content"
            android:id="@+id/btnSpeak"
            android:background="@null"
            android:src="@drawable/mic"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_marginTop="10dp"
            android:text="@string/tap_on_mic"
            android:textColor="#FFFFFF"
            android:textSize="15dp"
            android:textStyle="normal"
            android:layout_height="wrap_content" />
    </LinearLayout>

</RelativeLayout>

NOTES :

tools:context : Customize with your package name.
tap_on_mic : The name of the microphone image you have downloaded.
tap_on_hear : The name of the speaker image you have downloaded.





Now we enter the code in the Main_Activity.java file to run the program commands

package id.ndiappink.speechtotext;

import android.os.Bundle;
import java.util.ArrayList;
import java.util.Locale;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements TextToSpeech.OnInitListener {
    private TextToSpeech tts;
    private TextView txtSpeechInput,tapvoice;
    private ImageButton btnSpeak,btnvoicenya;
    private final int REQ_CODE_SPEECH_INPUT = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tts = new TextToSpeech(this, this);
        txtSpeechInput = (TextView)findViewById(R.id.txtSpeechInput);
        tapvoice = (TextView)findViewById(R.id.tapvoice);
        btnSpeak = (ImageButton)findViewById(R.id.btnSpeak);
        btnvoicenya = (ImageButton)findViewById(R.id.btnvoicee);
        btnvoicenya.setVisibility(View.GONE);
        tapvoice.setVisibility(View.GONE);
        btnvoicenya.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                speakOut();
            }
        });
        btnSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            promptSpeechInput();
            }
        });
    }

    @Override
    public void onDestroy(){
        if(tts!=null){
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }
    
    @Override
    public void onInit(int status) {
        if(status == TextToSpeech.SUCCESS){
            int result = tts.setLanguage(Locale.US);
            if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
                Log.e("TTS", "This Language is not supported");
            }
            else{
                btnSpeak.setEnabled(true);
                speakOut();
            }
        }
        else{
            Log.e("TTS", "Initilization Failed");
        }
    }

    private void speakOut(){
        String text = txtSpeechInput.getText().toString();
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

    private void promptSpeechInput(){
        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, getString(R.string.speech_prompt));

        try{
            startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
        }catch (ActivityNotFoundException a){
            Toast.makeText(getApplicationContext(),getString(R.string.speech_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_SPEECH_INPUT: {
                if (resultCode == RESULT_OK && null != data){
                    ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    txtSpeechInput.setText(result.get(0));
                    btnvoicenya.setVisibility(View.VISIBLE);
                    tapvoice.setVisibility(View.VISIBLE);
                }
            }
        }
    }
}

If there is no error, the program is ready to run.

For more details, please watch the video below


Powered by Blogger.