我想能獲得Android TTS API來讀我的「話語」,然後不成功調用onUtteranceCompleted()偵聽器。我已經註冊了我的TTS對象並返回了SUCCESS,所以我無法弄清楚爲什麼我的回調沒有被調用。的Android TTS onUtteranceCompleted回調是沒有得到所謂的
我試過尋找幫助,但似乎其他人也有這方面的困難。我錯過了一些簡單的東西嗎
感謝您提供任何幫助。
package com.test.mytts;
import java.util.HashMap;
import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.widget.TextView;
import android.widget.Toast;
public class MyTTS extends Activity implements OnInitListener, OnUtteranceCompletedListener
{
TextView tv;
private TextToSpeech _tts;
@Override
public void onCreate(Bundle savedInstanceState)
{
tv = new TextView(this);
tv.setText("MyTTS: ");
super.onCreate(savedInstanceState);
setContentView(tv);
_tts = new TextToSpeech(this, this);
}
@Override
public void onInit(int status)
{
HashMap<String, String> myHashAlarm = new HashMap<String, String>();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "test");
if (status == TextToSpeech.SUCCESS)
{
Toast.makeText(this, "Trying to speak...", Toast.LENGTH_SHORT).show();
int result = _tts.setOnUtteranceCompletedListener(this);
tv.append(String.valueOf(result));
_tts.setSpeechRate((float) .5);
_tts.speak("Testing one, two, three", TextToSpeech.QUEUE_ADD, myHashAlarm);
}
else
Toast.makeText(this, "Failed to initialize TTS.", Toast.LENGTH_SHORT).show();
}
@Override
public void onUtteranceCompleted(String utteranceId)
{
Toast.makeText(this, "onUtteranceCompleted", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy()
{
super.onDestroy();
_tts.shutdown();
}
}
我沒有看到你有什麼問題,但是你最好使用Log語句而不是Toast來跟蹤正在發生的事情。然後您可以查看logcat中的排序。 – 2011-01-11 00:28:58
我認爲這將有助於你:[http://stackoverflow.com/questions/4658376/how-to-know-when-tts-is-finished][1] [1]:HTTP: //stackoverflow.com/questions/4658376/how-to-know-when-tts-is-finished – uriellabs 2013-09-03 11:15:01