【问题标题】:Text To Speech speak() failure: not bound to the TTS engine in Android StudioText To Speech speak() 失败:未绑定到 Android Studio 中的 TTS 引擎
【发布时间】:2015-12-19 13:46:46
【问题描述】:

我在尝试使用 Text To Speech 时遇到错误。
我有一个按钮,当我单击它时,我的 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();

}
   } 

【问题讨论】:

  • 创建activity时,有没有看到“Initialization Failed!” logcat 中的错误消息?
  • 是的,先生。初始化失败。
  • 你是在真机还是模拟器上测试?什么是 API 级别?转到设置 -> 语言和输入 -> 文字转语音选项。它是否显示首选 TTS 引擎?点击“听一个例子”。有用吗?
  • @qbix。是的,我在我的手机上对其进行了测试,但它仍然无法正常工作。但我尝试直接打开我的 cityinfo 活动,我还手动将字符串设置为语音,它可以工作。但是当我以我必须的方式打开城市信息时,它不再起作用了。顺便说一句,我的 cityinfo 在我的 tabhost 里面。
  • 您的回复表明 TTS 在一种情况下有效。这确认您有一个正常工作的 TTS 引擎。很难提供更多帮助,因为您的回复说故障发生在您未发布的代码中。我只能建议您检查您的代码,以确保在创建TextToSpeech 对象后,您在尝试生成语音之前从onInit() 获得成功状态。对于调试,添加并记录一个布尔值以捕获初始化状态。我注意到在您发布的代码中,您在onPause() 中关闭了 TTS,但没有在onResume() 中重新启动它。这是一个错误——也许不是关键问题

标签: android android-studio text-to-speech android-speech-api


【解决方案1】:

试试这个,

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

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

这是onActivityResult时的代码,在运行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);
         }
     }
}

【讨论】:

    猜你喜欢
    • 2012-07-31
    • 1970-01-01
    • 2014-02-25
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2021-01-24
    相关资源
    最近更新 更多