首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在TextToSpeech操作完成时实现振动

在TextToSpeech操作完成时实现振动
EN

Stack Overflow用户
提问于 2013-04-19 08:08:37
回答 1查看 115关注 0票数 0

我正在做一个项目,当TextToSpeech函数完成一条消息时,需要通过振动来发出警报。我已经实现了TextToSpeech函数,并且知道如何创建振动,但我不确定在哪里编写vibration.Also,我遇到的关于如何实现OnUtteranceCompleted方法的示例让我陷入了无可救药的困惑。有没有人能帮我把OnUtteranceCompleted函数组合在一起,以及在哪里插入振动代码?下面是我的代码:

公共类TypeNewMessageActivity扩展了Activity实现的TextToSpeech.OnInitListener{

代码语言:javascript
复制
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);


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-19 08:36:40

如果您想在playText()完成发言时震动,那么请按如下所示进行更改

代码语言:javascript
复制
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);
}  

然后

代码语言:javascript
复制
@Override
public void onUtteranceCompleted(String utteranceId)
{
    // code to vibrate.
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16095121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档