0

我正在开发一个项目,该项目需要在 TextToSpeech 函数完成消息时通过振动发出警报。我已经实现了 TextToSpeech 函数,并且知道如何创建振动,但我不确定在哪里编写振动代码。此外,我遇到的关于如何实现 OnUtteranceCompleted 方法的示例让我感到非常困惑。谁能帮我把 OnUtteranceCompleted 函数放在一起,以及在哪里插入振动代码?这是我的代码:

公共类 TypeNewMessageActivity 扩展 Activity 实现 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);


}
4

1 回答 1

1

如果您想在 playText() 讲话结束时振动,请按以下方式更改

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

然后

@Override
public void onUtteranceCompleted(String utteranceId)
{
    // code to vibrate.
}
于 2013-04-19T00:36:40.420 回答