我正在开发一个应用程序,它需要使用Text to speech类才能读取文本。事情是初始化非常慢,大约4秒。我遵循了本教程:http://code.tutsplus.com/tutorials/use-text-to-speech-on-android-to-read-out-incoming-messages--cms-22524。我做了一些研究,但我没有找到真正好的答案,或者我想念它们。
例如,我通过在一个线程中调用我的checkTTS函数来尝试这个solution但没有结果可见。 Here和here,他们说我的具有意图的checkTTS功能可能没那么有用,我可以看看我所需要的语言是否可用。
所以我的问题:
这是我的活动代码:
public class MyActivity extends Activity {
private Speaker _speaker;
private final int CHECK_CODE = 0x1;
private final int SHORT_DURATION = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("I'm just before checkTTS");
checkTTS();
}
private void checkTTS(){
System.out.println("I'm in checkTTS");
Intent check = new Intent();
check.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(check, CHECK_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CHECK_CODE){
if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
System.out.println("I'm in onActivityResult");
_speaker = new Speaker(this);
}else {
Intent install = new Intent();
install.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(install);
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
_speaker.destroy();
}
}
这是我的演讲班:
public class Speaker implements OnInitListener {
private TextToSpeech _tts;
private boolean _ready = false;
private boolean _allowed = false;
public Speaker(Context context){
_tts = new TextToSpeech(context, this);
}
public void setSpeedRate(float speechrate) {
_tts.setSpeechRate(speechrate);
}
public boolean isAllowed(){
return _allowed;
}
public void allow(boolean allowed){
_allowed = allowed;
}
@Override
public void onInit(int status) {
System.out.println("I'm in onInit from Speaker");
if(status == TextToSpeech.SUCCESS){
_tts.setLanguage(Locale.UK);
_ready = true;
} else{
_ready = false;
}
}
public void speak(String text){
if(_ready && _allowed) {
HashMap<String, String> hash = new HashMap<String,String>();
hash.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
_tts.speak(text, TextToSpeech.QUEUE_ADD, hash);
}
}
public boolean isSpeaking() {
return _tts.isSpeaking();
}
public void pause(int duration){
_tts.playSilence(duration, TextToSpeech.QUEUE_ADD, null);
}
public void stop() {
_tts.stop();
}
public void destroy(){
_tts.shutdown();
}
}
这是日食的日志:
15:14:58.897: D/dalvikvm(23966): GC_CONCURRENT freed ...
15:15:00.014: I/System.out(23966): I'm just before checkTTS
15:15:00.014: I/System.out(23966): I'm in checkTTS
15:15:00.046: D/AbsListView(23966): unregisterIRListener() is called
15-13 15:15:01.272: I/System.out(23966): I'm in onActivityResult
15:15:01.311: I/TextToSpeech(23966): Sucessfully bound to com.samsung.SMT
15:15:01.663: D/AbsListView(23966): onVisibilityChanged() is called, visibility : 4
15:15:01.663: D/AbsListView(23966): unregisterIRListener() is called
15:15:02.600: I/TextToSpeech(23966): Connected to ComponentInfo{com.samsung.SMT/...}
15:15:02.608: I/TextToSpeech(23966): onServiceConnected but waiting
15:15:02.608: I/System.out(23966): I'm in onInit from Speaker
15:15:02.616: I/System.out(23966): Speaker ready
15:15:02.616: I/TextToSpeech(23966): onServiceConnected waiting end
感谢您的帮助, 最好的祝福, Zed13
Ps:抱歉错误......
编辑:有趣的事实,它似乎用三星的声音比用谷歌的声音更快地启动......
答案 0 :(得分:0)
onCreate
方法中)并在检查available languages或if some language is supported后。由于您正在跳过一个步骤(ACTION_CHECK_TTS_DATA),如果一切都成功,您的初始化将更快。您可以检查系统looking to this code中安装的引擎,并使用该信息启动更准确的ACTION_CHECK_TTS_DATA意图。