0

TextToSpeech 기능이 메시지를 완료 할 때 진동을 통해 경고가 필요한 프로젝트 작업 중입니다. TextToSpeech 함수를 구현하고 진동을 만드는 방법을 알고 있지만 진동을 코딩 할 위치를 잘 모릅니다. 또한 OnUtteranceCompleted 메서드를 구현하는 방법에 대한 예가 절망적으로 혼란에 빠지게했습니다. 누구나 OnUtteranceCompleted 함수를 함께 배치 할 수있을뿐만 아니라 진동 코드를 어디에 삽입 할 수 있습니까? 여기 내 코드입니다 :TextToSpeech 동작 완료시 진동 구현

공용 클래스 TypeNewMessageActivity이 활동 TextToSpeech.OnInitListener를 구현 확장 {당신이 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); 
} 

을 따를 변경 후 말 할 때마다 진동하려면

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


} 

답변

1

@Override 
public void onUtteranceCompleted(String utteranceId) 
{ 
    // code to vibrate. 
} 
+0

대단히 감사합니다! – embersofadyingfire

+0

Hoan : OnUtteranceCompleted 내에서 Intent를 사용하려고하는데 작동하지 않습니다. 왜 이것이 가능한지에 대한 의견을 나에게 줄 수 있습니까? 내 의도 코드가 맞다는 것을 안다. 그래서 나는 틀린 것이 틀림 없다. 고맙다! – embersofadyingfire

+0

질문을 게시하여 잘못 된 것을 지적하면 나 또는 누군가가 대답 할 것입니다. –