Android 语音引擎的字符串输入

发布于 2024-08-16 21:41:49 字数 1933 浏览 4 评论 0原文

在我看来,类 TextToSpeech 的方法“speak”仅适用于方法 onInit 或 onUtteranceCompleted。但是,onInit 和 onUtteranceCompleted 没有任何用于传递字符串的参数。

在下面的代码中,我尝试在方法之外定义一个全局字符串数组列表,并使用数组列表作为字符串输入。由于某种原因,它没有成功。但引擎确实会说“你睡得好吗”。任何帮助表示赞赏。

public class TTS extends Activity implements OnInitListener,OnUtteranceCompletedListener,Runnable  {

    ArrayList<String> content=new ArrayList<String>();    
    int MY_DATA_CHECK_CODE=50;
 private TextToSpeech mTts;

公共无效onCreate(捆绑保存实例状态){

        content.add("test");
        content.add("another test");
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.splash);

        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) {

        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            // success, create the TTS instance
            mTts = new TextToSpeech(this,this);



        } else {
            // missing data, install it

            Intent installIntent = new Intent();
            installIntent.setAction(
            TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }
}


  public void  onInit(int status){
 if(status==TextToSpeech.SUCCESS){
  mTts.setLanguage(Locale.US);
  mTts.setOnUtteranceCompletedListener(this);

             String myText1 = "Did you sleep well?";
             mTts.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);

            for(int i=0;i<content.size();i++){
           mTts.speak(content.get(i),TextToSpeech.QUEUE_ADD,null);
            }


           if(status==TextToSpeech.ERROR){
  mTts.shutdown();
          }
        }

}

It seems to me that the method "speak" of class TextToSpeech only works in method onInit or onUtteranceCompleted. However, onInit and onUtteranceCompleted don't have any parameter for passing strings.

In the following code, I tried to define a global string arraylist outside the methods and used the arraylist for string input.For some reason , it didn't work out.But the engine did speak "did you sleep well". Any help is appreciated.

public class TTS extends Activity implements OnInitListener,OnUtteranceCompletedListener,Runnable  {

    ArrayList<String> content=new ArrayList<String>();    
    int MY_DATA_CHECK_CODE=50;
 private TextToSpeech mTts;

public void onCreate(Bundle savedInstanceState) {

        content.add("test");
        content.add("another test");
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.splash);

        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) {

        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            // success, create the TTS instance
            mTts = new TextToSpeech(this,this);



        } else {
            // missing data, install it

            Intent installIntent = new Intent();
            installIntent.setAction(
            TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }
}


  public void  onInit(int status){
 if(status==TextToSpeech.SUCCESS){
  mTts.setLanguage(Locale.US);
  mTts.setOnUtteranceCompletedListener(this);

             String myText1 = "Did you sleep well?";
             mTts.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);

            for(int i=0;i<content.size();i++){
           mTts.speak(content.get(i),TextToSpeech.QUEUE_ADD,null);
            }


           if(status==TextToSpeech.ERROR){
  mTts.shutdown();
          }
        }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶底世界 2024-08-23 21:41:49

我相信您的某些代码丢失了,但仅供参考,可以通过参数映射为话语分配 ID,例如:

HashMap<String, String> myHashAlarm = new HashMap();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ID of First Utterance");
mTts.speak("It was a clear black night", TextToSpeech.QUEUE_ADD, myHashAlarm);

“第一个话语的 ID”将传递给 onUtteranceCompleted(String utteranceId)

请参阅使用文本转语音

I believe some of your code is missing, but FYI it is possible to assign an ID to an utterance via the parameters map, e.g.:

HashMap<String, String> myHashAlarm = new HashMap();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ID of First Utterance");
mTts.speak("It was a clear black night", TextToSpeech.QUEUE_ADD, myHashAlarm);

"ID of First Utterance" will be passed to onUtteranceCompleted(String utteranceId)

Please see Using Text-to-Speech.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文