我是Android开发,Eclipse和Java的新手(迄今为止主要完成.Net和IVR编程),所以当我试图编译和运行我发现的TTS示例应用程序在Droid上,我并不感到惊讶,我马上得到了运行时错误。错误是:未解决Android开发的TextToSpeech类
dalvikvm无法解析com/sample/TTSapp/AndroidTTSapp;界面4'android/speech/tts/TextToSpeech $ OnInitListner;'
我想OnInitListner方法必须在我安装Android SDK时安装的一个类中(我相信1.6版R1),但我不确定如何将关联的类模块导入到目前的计划。我无法在系统中的任何位置找到语音/ tts/TextToSpeech目录。我需要从某处下载此目录吗?以下是我尝试运行的演示TTS程序的Java源代码:
package com.sample.TTSApp;
import android.app.Activity; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.util.Log; import android.view.View; import android.widget.Button; import com.sample.TTSApp.R; import java.util.Locale; import java.util.Random; public class AndroidTTSapp extends Activity implements TextToSpeech.OnInitListener private static final String TAG =“TextToSpeechDemo”;
private TextToSpeech mTts; 私人按钮mAgainButton; (保存实例状态) @Override setContentView(R.layout.main); //初始化文本到语音转换。这是一个异步操作。
//初始化完成后调用OnInitListener(第二个参数)。 //实例化TextToSpeech.OnInitListener
mTts = new TextToSpeech(this,this); mAgainButton =(Button)findViewById(R.id.again_button);
mAgainButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
sayHello();
}
});
}
@Override
public void onDestroy()
{ // Don't forget to shutdown!
if (mTts != null)
{
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
} // 实现TextToSpeech.OnInitListener。
public void onInit(int status)
{
// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
if (status == TextToSpeech.SUCCESS)
{
int result = mTts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
{
// Language data is missing or the language is not supported.
Log.e(TAG, "Language is not available.");
}
else
{
// Check the documentation for other possible result codes. For example, the language may be available for the locale
// but not for the specified country and variant.
// The TTS engine has been successfully initialized. Allow the user to press the button for the app to speak again.
mAgainButton.setEnabled(true);
// Greet the user
sayHello();
}
} 其他 {// 初始化失败。 Log.e(TAG,“无法初始化TextToSpeech。”); } };
private static final Random RANDOM = new Random();
private static final String[] HELLOS =
{
"Hello World", "This is Text to speech demo by Zahid Shaikh"
};
private void sayHello()
{
// Select a random hello.
int i =0;
int helloLength = HELLOS.length;
String hello = HELLOS[i];
i++;
if(i == helloLength) i =0;
mTts.speak(hello,TextToSpeech.QUEUE_FLUSH,null);
}
}
预先感谢任何assitance谁能给初学者喜欢自己。
唐特利