Android : Text to Speech with Language Changer Option

Text to Speech with Language Changer Option

In this post we are going to make a Android Application using the text to speech feature. What differs this tutorial with the other is this tutorial uses a language changing option. So the text can be spoken in different accent based on the country selected.



This is the source code for main source in java:
package org.agung.text2speech;


import java.util.Locale;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainSpeak extends Activity implements OnInitListener {
    String        m_strInputText;
    EditText     m_edtInputText;
    Button         m_btnSpeak;
    Toast         m_toast;

    private TextToSpeech m_TTS;
    private int MY_CHECK_DATA = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_speak);
        m_edtInputText = (EditText)findViewById(R.id.edtInputText);
        m_btnSpeak     = (Button)findViewById(R.id.btnSpeak);
        Intent m_checkTTSIntent = new Intent();
        m_checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(m_checkTTSIntent, MY_CHECK_DATA);
        m_btnSpeak.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View argView) {
                // TODO Auto-generated method stub
                m_strInputText =  m_edtInputText.getText().toString();
                speakText(m_strInputText);
            }
        });
    }
   
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == MY_CHECK_DATA){
            if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
                m_TTS = new TextToSpeech(this,this);
            }
            else
            {
                Intent m_installTTSIntent = new Intent();
                m_installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(m_installTTSIntent);
            }
        }
    }

    private void speakText(String text){
        m_TTS.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main_speak, menu);
        return true;
    }
   
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId())
        {
            case R.id.langEnglish:
                m_TTS.setLanguage(Locale.US);
                m_toast = Toast.makeText(MainSpeak.this, "English", Toast.LENGTH_SHORT);
                m_toast.setGravity(Gravity.CENTER, 0, 0);
                m_toast.show();
                return true;
            case R.id.langGerman:
                m_TTS.setLanguage(Locale.GERMAN);
                m_toast = Toast.makeText(MainSpeak.this, "German", Toast.LENGTH_SHORT);
                m_toast.setGravity(Gravity.CENTER, 0, 0);
                m_toast.show();
                return true;
            case R.id.langFrench:
                m_TTS.setLanguage(Locale.FRENCH);
                m_toast = Toast.makeText(MainSpeak.this, "French", Toast.LENGTH_SHORT);
                m_toast.setGravity(Gravity.CENTER, 0, 0);
                m_toast.show();
                return true;
            case R.id.langItalian:
                m_TTS.setLanguage(Locale.ITALIAN);
                m_toast = Toast.makeText(MainSpeak.this, "Italian", Toast.LENGTH_SHORT);
                m_toast.setGravity(Gravity.CENTER, 0, 0);
                m_toast.show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }
    @Override
    public void onInit(int status) {
        // TODO Auto-generated method stub
        if(status == TextToSpeech.SUCCESS)
        {
            m_TTS.setLanguage(Locale.US);
            Log.e("TTS INIT", "TTS SUCCESS");
        }
        else if(status == TextToSpeech.ERROR)
        {
            Log.e("TTS INIT", "TTS ERROR");
        }
    }
}


This is the layout xml source code:
<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"
    tools:context=".MainSpeak" >

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="Text to Speech Test"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/edtInputText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/tvTitle"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnSpeak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edtInputText"
        android:layout_centerHorizontal="true"
        android:text="Speak" />

</RelativeLayout>


This is the xml source code for the menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/chooseLang" android:title="@string/choose_lang">
        <menu>
            <item android:id="@+id/langEnglish" android:title="@string/lang_en" android:icon="@drawable/us"/>
            <item android:id="@+id/langGerman" android:title="@string/lang_de" android:icon="@drawable/de"/>
            <item android:id="@+id/langFrench" android:title="@string/lang_fr" android:icon="@drawable/fr"/>
            <item android:id="@+id/langItalian" android:title="@string/lang_it" android:icon="@drawable/it"/>
        </menu>
    </item>
</menu>

If you want to download the entire source code, you can obtain it via mediafire. (LINK)

No comments:

Post a Comment