Quantcast
Viewing latest article 3
Browse Latest Browse All 10

Android Dev: Simple Text to Speech

{lang: 'en-GB'}


Image may be NSFW.
Clik here to view.

Here is a quick example, with sample Eclipse project, of how to simply use the inbuilt Text to Speech (TTS) in Android.

There are a few steps that I have tried to explain briefly in comments, including checking that the TTS service is installed (it is on most phones), setting the locale (language), etc.

import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class main extends Activity implements TextToSpeech.OnInitListener {
	
	private TextToSpeech mTts;
    // This code can be any value you want, its just a checksum.
    private static final int MY_DATA_CHECK_CODE = 1234;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button english = (Button)findViewById(R.id.button2);
        Button spanish = (Button)findViewById(R.id.button3);
        Button french = (Button)findViewById(R.id.button4);
        Button german = (Button)findViewById(R.id.button5);
        Button italian = (Button)findViewById(R.id.button6);
        
        //Check if TTS is installed
        Intent checkIntent = new Intent();
    	checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    	startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
    	
    	Context context = getApplicationContext();
        CharSequence text = "Checking if TTS is installed";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        
        
        english.setOnClickListener(new View.OnClickListener()
        {

                public void onClick(View v)
                {

                	//set language locale
                	mTts.setLanguage(Locale.UK);
                	//speak given text
                	mTts.speak("Giraffe",
                			TextToSpeech.QUEUE_FLUSH,
                            null);
                             	
                }
        });
        
        french.setOnClickListener(new View.OnClickListener()
        {

                public void onClick(View v)
                {

                	//set language locale
                	mTts.setLanguage(Locale.FRENCH);
                	//speak given text
                	mTts.speak("girafe",
                			TextToSpeech.QUEUE_FLUSH,
                            null);
                             	
                }
        });
        
        spanish.setOnClickListener(new View.OnClickListener()
        {

                public void onClick(View v)
                {

                	//set language locale
                	mTts.setLanguage(new Locale("spa"));
                	//speak given text
                	mTts.speak("jirafa",
                			TextToSpeech.QUEUE_FLUSH,
                            null);
                             	
                }
        });
        
        german.setOnClickListener(new View.OnClickListener()
        {

                public void onClick(View v)
                {
   
                	//set language locale
              	     mTts.setLanguage(Locale.GERMAN);
              	    //speak given text
              	     mTts.speak("Giraffe",
                			TextToSpeech.QUEUE_FLUSH,
                            null);
               	
                             	
                }
        });
        
        italian.setOnClickListener(new View.OnClickListener()
        {

                public void onClick(View v)
                {

                	//set language locale
                	mTts.setLanguage(Locale.ITALIAN);
                	//speak given text
                	mTts.speak("giraffa",
                			TextToSpeech.QUEUE_FLUSH,  // Drop all pending entries in the playback queue.
                            null);
                             	
                }
        });
        
        
        
    }
    
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == MY_DATA_CHECK_CODE)
        {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
            {
                // TTS installed, create instance
                mTts = new TextToSpeech(this, this);
                Context context = getApplicationContext();
                CharSequence text = "TTS is installed";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
                
            }
            else
            {
                // TTS not installed, install it
            	
            	Context context = getApplicationContext();
                CharSequence text = "TTS is NOT installed";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            	
                Intent installIntent = new Intent();
                installIntent.setAction(
                        TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
        }
    }

	@Override
	public void onInit(int status)
	{
	
	}
	
	@Override
    public void onDestroy()
    {
        // Close Text to Speech Service
        if (mTts != null)
        {
            mTts.stop();
            mTts.shutdown();
        }
        super.onDestroy();
    }
}

Click here to download the sample project.


Image may be NSFW.
Clik here to view.

The post Android Dev: Simple Text to Speech appeared first on Droid App.


Viewing latest article 3
Browse Latest Browse All 10

Trending Articles