【发布时间】:2011-06-06 21:09:10
【问题描述】:
我试图让 Android TTS API 读取我的“话语”,然后调用 onUtteranceCompleted() 侦听器失败。我已经注册了我的 TTS 对象并且它返回了 SUCCESS,所以我一生都无法弄清楚为什么我的回调没有被调用。
我尝试过寻求帮助,但似乎其他人也遇到了困难。我错过了一些简单的东西吗?
感谢您提供的任何帮助。
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 语句而不是 toasts 来跟踪正在发生的事情。然后你可以在 logcat 中查看排序。
-
我认为这会对你有所帮助:[stackoverflow.com/questions/4658376/… [1]: stackoverflow.com/questions/4658376/…