Text To Speech speak()失败:未绑定到Android Studio中的TTS引擎



我在尝试使用"文本到语音"时出错
我有一个按钮,当我点击它时,我的logcat中出现了一个错误,说

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

这是我的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();
}
   } 

试试这个,

这对于检查TTS引擎是否不可用很有用

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

这是在运行tts.speak()之前打开ActivityResult时的代码

@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);
         }
     }
}

最新更新