Dipak Ahmedabad
Dipak Ahmedabad

Reputation: 55

How to Integrate Text to Speech Functionality?

How to Integrate Text to Speech Functionality?

Following is my code:-

public class TtsActivity extends Activity implements OnInitListener {

    private int MY_DATA_CHECK_CODE = 0;

    private TextToSpeech tts;

    private EditText inputText;
    private Button speakButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        inputText = (EditText) findViewById(R.id.input_text);
        speakButton = (Button) findViewById(R.id.speak_button);

        speakButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = inputText.getText().toString();
                if (text != null && text.length() > 0) {
                    Toast.makeText(TtsActivity.this, "Saying: " + text,
                            Toast.LENGTH_LONG).show();
                    tts.speak(text, TextToSpeech.QUEUE_ADD, null);
                }
            }
        });

        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_DATA_CHECK_CODE) {
            System.out.println("Code is:- " + requestCode);
            System.out.println("Code is:- " + resultCode);
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                System.out.println("Code is:- " + resultCode);
                // success, create the TTS instance
                tts = new TextToSpeech(this, this);
            } else {
                System.out.println("Code is:- " + resultCode);
                // missing data, install it
                Intent installIntent = new Intent();
                installIntent
                        .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
        }

    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            Toast.makeText(TtsActivity.this,
                    "Text-To-Speech engine is initialized", Toast.LENGTH_LONG)
                    .show();
        } else if (status == TextToSpeech.ERROR) {
            Toast.makeText(TtsActivity.this,
                    "Error occurred while initializing Text-To-Speech engine",
                    Toast.LENGTH_LONG).show();
        }
    }
}

if i run above code the Google android market is open and if i install the application then the text to speech is work complete but if i not install the application the error is occured.

So, Please Help Me.

Upvotes: 0

Views: 2420

Answers (1)

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

That is because Text To Speech needs TTS engine on your device.If you don't have that installed on your device,it will redirect you to market and let you download the same.So after you install that engine,your app would find TTS Engine and works fine on your device.

For more reference,please visit:

http://www.jameselsey.co.uk/blogs/techblog/android-a-really-easy-tutorial-on-how-to-use-text-to-speech-tts-and-how-you-can-enter-text-and-have-it-spoken/

EDIT :

TextToSpeech mTts;
mTts = new TextToSpeech(this,
            this  // TextToSpeech.OnInitListener
            );
...
mTts.setPitch(2.3); //change it as per your need
...
mTts.speak(s,TextToSpeech.QUEUE_FLUSH,null);

Upvotes: 3

Related Questions