2012-06-19 119 views
0

我正在嘗試創建一個文本到語音應用程序,該應用程序會在應用程序暫停時以逗號的形式記住最後一句或部分句子。以下是我的代碼。文本到語音Android

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    pitch = (EditText) findViewById(R.id.pitch); 
    words = (EditText) findViewById(R.id.wordsToSpeak); 
    words.setText("This message is intended only for the use of the individual or entity to which it is addressed and may contain information that is privileged, confidential or exempt from disclosure by law."); 
    speakBtn = (Button) findViewById(R.id.speak); 

    // Check to be sure that TTS exists and is okay to use 
    Intent checkIntent = new Intent(); 
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 
    startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK); 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQ_TTS_STATUS_CHECK) { 
     switch (resultCode) { 
     case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS: 
      // TTS is up and running 
      mTts = new TextToSpeech(this, this); 
      Log.v(TAG, "Pico is installed okay"); 
      break; 
     case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA: 
     case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA: 
     case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME: 
      // missing data, install it 
      Log.v(TAG, "Need language stuff: " + resultCode); 
      Intent installIntent = new Intent(); 
      installIntent 
        .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); 
      startActivity(installIntent); 
      break; 
     case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL: 
     default: 
      Log.e(TAG, "Got a failure. TTS apparently not available"); 
     } 
    } else { 
     // Got something else 
    } 
} 

@Override 
public void onInit(int status) { 
    // Now that the TTS engine is ready, we enable the button 
    if (status == TextToSpeech.SUCCESS) { 
     speakBtn.setEnabled(true); 
     mTts.setOnUtteranceCompletedListener(this); 

    } 
} 

public void doSpeak(View view) { 
    mTts.setPitch(new Float(pitch.getText().toString())); 

    StringTokenizer st = new StringTokenizer(words.getText().toString(), 
      ",."); 

    while (st.hasMoreTokens()) { 

     params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, 
       String.valueOf(uttCount++)); 
     mTts.speak(st.nextToken(), TextToSpeech.QUEUE_ADD, params); 
    } 

    // mTts.speak(words.getText().toString(), TextToSpeech.QUEUE_ADD, null); 
}; 

@Override 
public void onPause() { 
    super.onPause(); 
    // if we're losing focus, stop talking 
    if (mTts != null) 
     mTts.stop(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    mTts.shutdown(); 
} 

@Override 
public void onUtteranceCompleted(String utteranceId) { 
    Log.v(TAG, "Got completed message for uttId: " + utteranceId); 
    lastUtterance = Integer.parseInt(utteranceId); 

} 

}

我能夠得到獲得了Android說話和跟蹤令牌,它最後的評價。但是,當您按下speakBtn時,我不知道如何恢復最後一次停止的位置。 如果所有的標記沒有被成功地讀出來,是否還有回到標記器中的某個標記?

回答

0

如何:

for(int i=0; i<successfulUtterances; ++i) 
    tokenizer.nextToken(); 
String nextUnspokenUtterance = tokenizer.nextToken(); 

如果你問是否有直接的方式,沒有。但這種方式將擺脫你不需要的所有令牌,讓你繼續。