简体   繁体   中英

Text To Speech speak() failure: not bound to the TTS engine in Android Studio

I'm getting an error when trying to use Text To Speech.
I have a Button and when I click it I'm getting an error in my logcat saying

E/TexttoSpeech: speak failed: not bound to TTS Engine. 

Here's my cityinfo.class

public class CityInfo extends Activity implements View.OnClickListener{
SharedPreferences.Editor editor;
TextView cityname1, cityarea,citypopulation,cityregion,cityprovince,cityinfo1;
String city_name, city_info,city_area,city_population,city_region,city_province,speech;
ImageView gallery;
int gallery_grid_Images[]={R.drawable.pic10,R.drawable.pic11,R.drawable.pic12,
        R.drawable.pic9,R.drawable.login_pic1,R.drawable.login_pic2,R.drawable.login_pic3,
        R.drawable.login_pic4,R.drawable.login_pic5
};
Button playb;
ImageButton audio;
ViewFlipper viewFlipper;
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_city_info);

    cityname1 = (TextView) findViewById(R.id.nameofcity);
    cityinfo1 = (TextView) findViewById(R.id.cityinfo);
    cityarea = (TextView) findViewById(R.id.area);
    citypopulation = (TextView) findViewById(R.id.population);
    cityregion = (TextView) findViewById(R.id.region);
    cityprovince = (TextView) findViewById(R.id.province);
   // SharedPreferences citypref = getApplicationContext().getSharedPreferences("CityPref", MODE_PRIVATE);
   // editor = citypref.edit();
  //  city_name = citypref.getString("nameofcity",null);
   // cityname1.setText(city_name);
  playb=(Button)findViewById(R.id.playb);
    playb.setOnClickListener(this);
    audio = (ImageButton) findViewById(R.id.play);
    Bundle extras = getIntent().getExtras();
   // audio.setOnClickListener(this);
    gallery=(ImageView)findViewById(R.id.gallery);
   // gallery.setOnClickListener(this);
    viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
    List<Integer> pictures = new ArrayList<Integer>();
    for (int index = 0; index < gallery_grid_Images.length; index++)
    {
        pictures.add(gallery_grid_Images[index]);
    }
    Collections.shuffle(pictures);
    for(int i=0;i<pictures.size();i++)
    {
        //  This will create dynamic image view and add them to ViewFlipper
        setFlipperImage(pictures.get(i));
    }


    if (extras != null) {

        city_name = extras.getString("cityname");
       cityname1.setText(city_name);
        city_info = extras.getString("cityinfo");
        cityinfo1.setText(city_info);
        city_area = extras.getString("cityarea");
        cityarea.setText(city_area);
        city_population = extras.getString("citypopulation");
        citypopulation.setText(city_population);
        city_province = extras.getString("cityprovince");
        cityprovince.setText(city_province);
        city_region = extras.getString("cityregion");
        cityregion.setText(city_region);
    }
    tts=new TextToSpeech(CityInfo.this, new TextToSpeech.OnInitListener() {

        @Override
        public void onInit(int status) {
            // TODO Auto-generated method stub
            if(status == TextToSpeech.SUCCESS){
                int result=tts.setLanguage(Locale.US);
                if(result==TextToSpeech.LANG_MISSING_DATA ||
                        result==TextToSpeech.LANG_NOT_SUPPORTED){
                    Log.e("error", "This Language is not supported");
                }
                else{
                    ConvertTextToSpeech();
                }
            }
            else
                Log.e("error", "Initilization Failed!");
        }
    });

}

private void setFlipperImage(int res) {
    Log.i("Set Filpper Called", res + "");
    ImageView image = new ImageView(getApplicationContext());
    image.setBackgroundResource(res);
    viewFlipper.addView(image);
    viewFlipper.startFlipping();
}


@Override
protected void onPause() {
    // TODO Auto-generated method stub

    if(tts != null){

        tts.stop();
        tts.shutdown();
    }
    super.onPause();
}



private void ConvertTextToSpeech() {
    // TODO Auto-generated method stub
    speech= "Chelsie Denise Malate";
    if(speech==null||"".equals(speech))
    {
        speech = "Content not available";
        tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
    }else
        tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}

@Override
public void onClick(View v) {
        ConvertTextToSpeech();

}
   } 

Try this,

This is useful to check whether a TTS engine is not available

Intent TTSIntent = new Intent();
TTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(TTSIntent, 2);

This is the code when onActivityResult, before running tts.speak ()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

     if(requestCode == 2) {
         if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
             try{
                 TTS = new TextToSpeech(getApplicationContext(), this);
                 TTS.setOnUtteranceProgressListener(uPL);
             } catch(Throwable throwable){
                 Log.e("Throwable", throwable.getMessage());
             }
         } else {
             Intent m_installTTSIntent = new Intent();
             m_installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
             startActivity(m_installTTSIntent);
         }
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM