我已将一个使用TTS API的活动和我在另一个项目中使用的代码放在一起,用于同一目的。但是,TTS无法运行,活动无法一起启动。我研究了我的代码,无法破译问题所在。代码与我的其他项目代码完全匹配,另一个项目中的代码按预期工作。 log cat数据很难理解,所以希望这个社区中的某个人可以帮我找到问题。我认为这可能是Android Studio问题。另一个项目安装在Eclipse Luna中。它必须是TTS代码,因为在我添加TTS代码之前,有问题的活动正好启动。我附上了我的代码和日志猫信息。
附加信息:也许这是我试图实施的行为。我试图在将活动加载到视图中时将保存的字符串发送到TTS引擎,使用计时器在视图初始化后1秒启动TTS。但是,我不确定是否可以在没有按下按钮的情况下启动TTS实例来执行它。我的另一个项目,只需点击一下按钮即可调用TTS引擎...... ???
public class InfoGreetView extends ActionBarActivity implements TextToSpeech.OnInitListener, TextToSpeech.OnUtteranceCompletedListener {
TextView infoGreetPrompt = (TextView) findViewById(R.id.infoGreetPrompt);
TextToSpeech textToSpeech;
Timer timer = new Timer();
Timer timer2 = new Timer();
TimerTask timerTask;
TimerTask timerTask2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info_greet_view);
textToSpeech = new TextToSpeech(this,this);
timerTask = new TimerTask() {
@Override
public void run() {
Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
startPlayback();
}
};
timer.schedule(timerTask,1000);
}
@Override
public void onDestroy(){
if (textToSpeech != null){
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status){
if (status == TextToSpeech.SUCCESS){
int result = textToSpeech.setLanguage(Locale.getDefault());
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
Toast.makeText(getApplicationContext(), R.string.messagePlaybackInterrupted, Toast.LENGTH_LONG).show();
}
}
else{
Toast.makeText(getApplicationContext(), R.string.messagePlaybackInterrupted, Toast.LENGTH_LONG).show();
}
}
public void startPlayback(){
String infoGreetViewPrompt = infoGreetPrompt.getText().toString();
HashMap<String, String> myHashRender = new HashMap<String, String>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "completed");
textToSpeech.speak(infoGreetViewPrompt, TextToSpeech.QUEUE_FLUSH, myHashRender);
}
@Override
public void onUtteranceCompleted(String utteranceId){
timerTask2 = new TimerTask() {
@Override
public void run() {
Intent displayGuestMessageView = new Intent(InfoGreetView.this, GuestMessageView.class);
startActivity(displayGuestMessageView);
}
};
timer2.schedule(timerTask2, 1000);
}
答案 0 :(得分:0)
万岁!我弄清楚问题是什么。首先,我在Android Studio中的依赖关系没有完成。我不得不添加编译&#39; com.android.support:support-v4:20.0.0&#39; jar;我所拥有的只有编译&com; android.support:appcompat-v7:20.0.0&#39; 。我相信这就是TTS没有运作的原因。
其次,我试图模仿Xcode的viewDidAppear方法和一个计时器,以便在视图出现时,在onResume()内开始TTS。但是,这不起作用。 Log Cat说了一些关于不绑定TTS的事情。所以,我将我的计时器扔进onCreate()而不是将我的startPlayback()方法与我的计时器中的调用分开,我只是将startPlayback()中的代码添加到我的计时器中。为什么?出于某种原因,电话没有发生。这是我新的完美操作代码!
public class InfoGreetView extends ActionBarActivity implements TextToSpeech.OnInitListener, TextToSpeech.OnUtteranceCompletedListener {
TextView infoGreetPrompt;
TextToSpeech textToSpeech;
ImageView attentionButton;
Timer timer = new Timer();
TimerTask timerTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info_greet_view);
attentionButton = (ImageView) findViewById(R.id.attentionButton);
infoGreetPrompt = (TextView) findViewById(R.id.infoGreetPrompt);
textToSpeech = new TextToSpeech(this,this);
timerTask = new TimerTask() {
@Override
public void run() {
String text = infoGreetPrompt.getText().toString();
HashMap<String, String> myHashRender = new HashMap<String, String>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "completed");
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, myHashRender);
}
};
timer.schedule(timerTask,500);
}
@Override
protected void onDestroy(){
super.onDestroy();
if (textToSpeech != null){
textToSpeech.stop();
textToSpeech.shutdown();
}
}
@Override
public void onInit(int status){
if (status == TextToSpeech.SUCCESS){
int result = textToSpeech.setLanguage(Locale.getDefault());
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
Toast.makeText(getApplicationContext(), R.string.languageNotSupported, Toast.LENGTH_LONG).show();
}
}
else{
Toast.makeText(getApplicationContext(), R.string.messagePlaybackInterrupted, Toast.LENGTH_LONG).show();
}
textToSpeech.setOnUtteranceCompletedListener(this);
}
@Override
public void onUtteranceCompleted(String utteranceId){
Intent displayGuestMessageView = new Intent(InfoGreetView.this, GuestMessageView.class);
startActivity(displayGuestMessageView);
}
}