Java 简单的TextToSpeech代码
我想做一个简单的Java 简单的TextToSpeech代码,java,android,Java,Android,我想做一个简单的TextToSpeechcode。我的代码在这里: TextToSpeech tts = new TextToSpeech(this, this); tts.setLanguage(Locale.US); tts.speak("This is a Alert Application", TextToSpeech.QUEUE_ADD,null,null); 但我得到了这个错误: Error:(100, 28) error: no suitable constructor foun
TextToSpeech
code。我的代码在这里:
TextToSpeech tts = new TextToSpeech(this, this);
tts.setLanguage(Locale.US);
tts.speak("This is a Alert Application", TextToSpeech.QUEUE_ADD,null,null);
但我得到了这个错误:
Error:(100, 28) error: no suitable constructor found for TextToSpeech(MainActivity,MainActivity)
constructor TextToSpeech.TextToSpeech(Context,OnInitListener,String) is not applicable
(actual and formal argument lists differ in length)
constructor TextToSpeech.TextToSpeech(Context,OnInitListener) is not applicable
(actual argument MainActivity cannot be converted to OnInitListener by method invocation conversion)
我错过了什么?我需要在代码中输入什么?将活动按以下方式实施:
public class AndroidTextToSpeech extends Activity implements TextToSpeech.OnInitListener {
在错误输出中,您必须按照建议使用有效参数初始化TextToSpeech对象,提供
上下文
和OnInitListener
对象
这是一个初始化示例(希望有帮助):
}
如果允许一个线程睡眠并且睡眠后正在调用tts.speak(),请尝试上面的代码。如果是这样的话,在查看代码的时候,tts似乎没有初始化,并且为空,因此会出现异常崩溃 这段代码应该可以防止异常,但是如果TTS引擎的初始化花费太长时间,那么您就不能让它说是加载。另外,我猜5秒(顺便说一句,这是一个很长的时间)睡眠是为了让它得到初始化 代码
这是我目前的代码: 包com.example.texttospeech
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences.Editor;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
公共类MainActivity扩展活动实现TextToSpeech.OnInitListener{
private int result = 0;
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
txtText = (EditText) findViewById(R.id.txtText);
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
speakOut();
}
});
}
// shutdown tts when activity destroy
@Override
public void onDestroy() {
// Don't forget to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
// It will called before TTS started
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
// check status for TTS is initialized or not
if (status == TextToSpeech.SUCCESS) {
// if TTS initialized than set language
result = tts.setLanguage(Locale.US);
// tts.setPitch(5); // you can set pitch level
// tts.setSpeechRate(); //you can set speech speed rate
// check language is supported or not
// check language data is available or not
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "Missing data", Toast.LENGTH_LONG).show();
// disable button
btnSpeak.setEnabled(false);
} else {
// if all is good than enable button convert text to speech
btnSpeak.setEnabled(true);
}
} else {
Log.e("TTS", "Initilization Failed");
}
}
// call this method to speak text
private void speakOut() {
String text = txtText.getText().toString();
if (result != tts.setLanguage(Locale.US)) {
Toast.makeText(getApplicationContext(), "Enter right Words...... ", Toast.LENGTH_LONG).show();
} else {
// speak given text
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
speak已被弃用implements activity with oninitlistener能否向我们展示完整的代码?从类开始,creationI没有创建类。我觉得这已经足够了。我错过了什么?我只想在应用程序启动时谈论一下“这是一个警报应用程序”。一切就绪:
公共类AndroidTextToSpeech扩展活动实现TextToSpeech.OnInitListener{TextToSpeech tts=new TextToSpeech(This,This);tts.setLanguage(Locale.US);tts.speak(“这是一个警报应用程序”,TextToSpeech.QUEUE_ADD,null,null);}
但现在setLanguage和speak是红色的,无法解析符号。您是否允许线程睡眠,睡眠后是否调用tts.speak()。如果是这样的话,此时查看您的代码,tts似乎没有初始化,并且为空,因此会出现异常崩溃。我尝试了此应用程序,但当我编写一个句子,然后单击speak按钮时,应用程序崩溃。speak已弃用。我想在Android ICS 4.0.1中尝试。问题是,当我构建应用程序时没有错误,但speak是w我在一个虚拟机中使用Android Studio,然后制作apk并将apk导出到手机。我看不到更多的错误。virtul设备是否有扬声器?为什么要在上面测试…!!在手机上试用。
public class mainj extends Activity implements OnInitListener {
private TextToSpeech myTTS;
// status check code
private int MY_DATA_CHECK_CODE = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loadscreen);
Intent checkTTSIntent = new Intent();
checkTTSIntent
.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
Thread logoTimer = new Thread() {
public void run() {
try {
try {
sleep(5000);
speakWords("loading");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//waiting for the sleep
splash_wait()
}
finally {
finish();
}
}
};
logoTimer.start();
}
// speak the user text
private void speakWords(String speech) {
// speak straight away
if(myTTS != null)
{
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
}
// act on result of TTS data check
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// the user has the necessary data - create the TTS
myTTS = new TextToSpeech(this, this);
} else {
// no data - install it now
Intent installTTSIntent = new Intent();
installTTSIntent
.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}
// setup TTS
public void onInit(int initStatus) {
// check for successful instantiation
if (initStatus == TextToSpeech.SUCCESS) {
if (myTTS.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE)
myTTS.setLanguage(Locale.US);
} else if (initStatus == TextToSpeech.ERROR) {
Toast.makeText(this, "Sorry! Text To Speech failed...",
Toast.LENGTH_LONG).show();
}
private void splash_wait() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences.Editor;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
private int result = 0;
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
txtText = (EditText) findViewById(R.id.txtText);
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
speakOut();
}
});
}
// shutdown tts when activity destroy
@Override
public void onDestroy() {
// Don't forget to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
// It will called before TTS started
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
// check status for TTS is initialized or not
if (status == TextToSpeech.SUCCESS) {
// if TTS initialized than set language
result = tts.setLanguage(Locale.US);
// tts.setPitch(5); // you can set pitch level
// tts.setSpeechRate(); //you can set speech speed rate
// check language is supported or not
// check language data is available or not
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "Missing data", Toast.LENGTH_LONG).show();
// disable button
btnSpeak.setEnabled(false);
} else {
// if all is good than enable button convert text to speech
btnSpeak.setEnabled(true);
}
} else {
Log.e("TTS", "Initilization Failed");
}
}
// call this method to speak text
private void speakOut() {
String text = txtText.getText().toString();
if (result != tts.setLanguage(Locale.US)) {
Toast.makeText(getApplicationContext(), "Enter right Words...... ", Toast.LENGTH_LONG).show();
} else {
// speak given text
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}