Pergunta

I'm working on a project that requires an alert through a vibration when the TextToSpeech function completes a message. I've implemented the TextToSpeech function, and know how to create a vibration, but I'm not sure where to code the vibration.Also, examples I've come across on how to implement the OnUtteranceCompleted method have left me hopelessly confused. Can anyone help me put the OnUtteranceCompleted function together, as well as where to insert the vibration code? Here is my code:

public class TypeNewMessageActivity extends Activity implements TextToSpeech.OnInitListener{

Button playButton;
EditText typeNewMessageEditText;
TextToSpeech tts;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_type_new_message);
    // Show the Up button in the action bar.
    setupActionBar();
}

/**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    playButton = (Button)findViewById(R.id.playButton);
    typeNewMessageEditText = (EditText)findViewById(R.id.typeNewMessageEditText);
    tts = new TextToSpeech (this, this);


    playButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            playText();
        }
    });
}

public void onDestroy(){

    if (tts != null){

        tts.stop();
        tts.shutdown();

    }

}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@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("tts", "This language is not supported");
        }else{


            playButton.setEnabled(true);
            playText();


        }
    }else{

        Log.e("tts", "Initialized failed");         
    }

}

public void playText(){

    String text = typeNewMessageEditText.getText().toString();
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);


}
Foi útil?

Solução

If you want to vibrate whenever playText() is done speaking then change as follow

public void playText(){

String text = typeNewMessageEditText.getText().toString();
HashMap<String, String> myHashRender = new HashMap<String, String>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "");
tts.speak(text, TextToSpeech.QUEUE_FLUSH, myHashRender);
}  

Then

@Override
public void onUtteranceCompleted(String utteranceId)
{
    // code to vibrate.
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top