string input for Android's speech engine

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

}


Asked by: Rafael522 | Posted: 20-01-2022






Answer 1

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.

Answered by: Arthur125 | Posted: 21-02-2022



Similar questions

android's ListView does not listen on single clicks

I'm developing an simple application on Android, where some items are shown on a list. The user may click on one, taking him to a further activity. Basics... But my OnItemClickListener does not get called! I've found this very similar question, but the solution (disallow the list item view to get focus) does n...


View like android's contact screen

I am developing an application with a large number of elements that must be ordered alphabetically, and I'd like it to have the same look and feel as android's contact list, That is [Letter] &lt;contact&gt; &lt;contact&gt; [Letter] &lt;contact&gt; &lt;contact&gt; &lt;contact&gt; etc. Which is the best way to achieve this same layout? I've seen several tutorials concerning scrollabl...


Is there a tool to allow the use of Android's 9-patches in Javascript?

I'd like a Javascript library that will stretch/scale 9-patch images correctly. Know of one?


Does Android's Intent use Googles cloud at all?

Sorry I'm new to Android handsets. When you do a startActivity(intent) does it involve hitting Googles cloud services at all to resolve the [handler] or is this purely a local call?


how to view all images in android's built- in gallery from a php page

I am developing an application to display all the images in android's built-in gallery in to a php page. I posted the uri (content://media/extrenal/images/media) of the gallery images to the php page. I was not able to recreate images by using webView's function loadUrl(). Should I need to upload the whole gallery to another directory and display the images? Or passing the bitmap itself wi...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top