3

Below is my code.I am unable to hear the voice in my kitkat device.Toast is appearing but voice is not playing.I am following this tutorial

https://www.tutorialspoint.com/android/android_text_to_speech.htm

package com.example.insert;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
import android.widget.Toast;
import android.util.Log;
import java.lang.Object;


public class SecondActivity extends AppCompatActivity {
    TextToSpeech t1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        String emailid;
        emailid = "Hi,say your email id";

        t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int result = t1.setLanguage(Locale.US);
                    if (result == TextToSpeech.LANG_MISSING_DATA
                            || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Toast.makeText(getApplicationContext(), "This language is not supported", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getApplicationContext(), "Initialization failed", Toast.LENGTH_SHORT).show();
                }

            }
        });


       // Toast.makeText(getApplicationContext(), emailid, Toast.LENGTH_SHORT).show();
        t1.speak(emailid, TextToSpeech.QUEUE_FLUSH, null);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Don't forget to shutdown tts!
        if (t1 != null) {
            Log.e("TTS","speech on destroy");
            t1.stop();
            t1.shutdown();
        }
    }
}

I have followed this post on stackoverflow.

Android TTS doesn't speak

But i didn't understand gameover,line and definition_string.

guys help me

Community
  • 1
  • 1
Lakshmi
  • 1,791
  • 3
  • 9
  • 10

4 Answers4

12

Here is my code. this code works on Nexus9.(Locale is Japan)

public class MainActivity extends AppCompatActivity{

    static final String TAG = "TTS";
    TextToSpeech mTts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String emailid;
        emailid = "こんにちは";

        mTts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int result = mTts.setLanguage(Locale.JAPAN);
                    if (result == TextToSpeech.LANG_MISSING_DATA
                            || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Toast.makeText(getApplicationContext(), "This language is not supported", Toast.LENGTH_SHORT).show();
                    }
                    else{
                        Log.v("TTS","onInit succeeded");
                        speak(emailid);
                    }
                } else {
                    Toast.makeText(getApplicationContext(), "Initialization failed", Toast.LENGTH_SHORT).show();
                }

            }
        });
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
    }

    void speak(String s){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Log.v(TAG, "Speak new API");
            Bundle bundle = new Bundle();
            bundle.putInt(TextToSpeech.Engine.KEY_PARAM_STREAM, AudioManager.STREAM_MUSIC);
            mTts.speak(s, TextToSpeech.QUEUE_FLUSH, bundle, null);
        } else {
            Log.v(TAG, "Speak old API");
            HashMap<String, String> param = new HashMap<>();
            param.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_MUSIC));
            mTts.speak(s, TextToSpeech.QUEUE_FLUSH, param);
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Don't forget to shutdown tts!
        if (mTts != null) {
            Log.v(TAG,"onDestroy: shutdown TTS");
            mTts.stop();
            mTts.shutdown();
        }
    }
}
Joe Mizuno
  • 437
  • 2
  • 15
1

It takes some time to initialise. Put the speak code in onClick method. If you trigger that method on button click, it will speak.

someButton.setOnClickListener(new OnClickListener{
onClick(View view){
t1.speak(emailid, TextToSpeech.QUEUE_FLUSH, null);
   }
}
Reeganth
  • 11
  • 2
0

I guess problem is with Locale. Please print logs. try out following code.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
import android.widget.Toast;


public class SecondActivity extends AppCompatActivity  {
    TextToSpeech t1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        String emailid;
        emailid="Hi,say your email id";

         t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status == TextToSpeech.SUCCESS) {
                    t1.setLanguage(Locale.US);
                    if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                         Log.d(TAG, "This Language is not supported");
                }
                }else{
                    Log.d(TAG, "Initilization Failed!");
               }

            }
        });


        Toast.makeText(getApplicationContext(), emailid,Toast.LENGTH_SHORT).show();
        t1.speak(emailid, TextToSpeech.QUEUE_FLUSH, null);

    }


@Override
 protected void onDestroy() {
    super.onDestroy();
    // Don't forget to shutdown tts!
    if (mTextToSpeech != null) {
        Log.d(TAG, "speach on destroy");
        t1.stop();
        t1.shutdown();
    }
}
Rohit
  • 2,646
  • 6
  • 27
  • 52
  • I have tried toast to see the output.I have edited the question.I have added the new code.Please check.No toast is appearing – Lakshmi Mar 10 '17 at 13:46
  • If no toast is appearing, I guess problem is with your String. Please change your string try to give some simple message is debug. In your String "Hi,say your email id" I guess ttf is not able to identify. Please try with "Hi, say your email id" or some simple string. – Rohit Mar 13 '17 at 04:51
0

I don't understand why didn't you implement your main Activity class from TextToSpeech.OnInitListener...!

Here's your answer:

import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidTextToSpeechActivity extends Activity implements
        TextToSpeech.OnInitListener {
    /** Called when the activity is first created. */

    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tts = new TextToSpeech(this, this);

        btnSpeak = (Button) findViewById(R.id.btnSpeak);

        txtText = (EditText) findViewById(R.id.txtText);

        // button on click event
        btnSpeak.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                speakOut();
            }

        });
    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        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 = txtText.getText().toString();

        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
}
Shobhit
  • 1,096
  • 11
  • 30