我已尝试在线进行文本到语音教程,但它给了我一个输入单词并单击按钮来说出键入单词的输出。
我真正想要的输出是应用程序只读取应用程序示例中显示的文本,它将阅读文本"我很高兴"字符串已经声明为TextView。
这是我想要做的一个例子的图片。
当您按下扬声器按钮时,它只会读取其上方的文字。
这是我的java文件,它仍在输入单词并发出语音:
package com.example.chadymaebarinan.emojiexpress;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.View;
import android.widget.EditText;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.content.Intent;
import android.widget.Toast;
import java.util.Locale;
public class Speech extends AppCompatActivity implements OnClickListener,OnInitListener {
private int MY_DATA_CHECK_CODE = 0;
private TextToSpeech myTTS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speech);
Button speakButton = (Button) findViewById(R.id.speak);
speakButton.setOnClickListener(this);
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
}
public void onClick(View v) {
//handle user clicks here
EditText enteredText = (EditText) findViewById(R.id.enter);
String words = enteredText.getText().toString();
speakWords(words);
}
private void speakWords(String speech) {
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
myTTS = new TextToSpeech(this, this);
} else {
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}
public void onInit(int initStatus) {
if (initStatus == TextToSpeech.SUCCESS) {
if (myTTS.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE)
myTTS.setLanguage(Locale.US);
myTTS.setLanguage(Locale.US);
} else if (initStatus == TextToSpeech.ERROR) {
Toast.makeText(this, "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
}
}
}
谢谢你们! :)
答案 0 :(得分:1)
在您的xml中创建textView
,其ID为myword
,然后从xml中移除editText
然后代替
public void onClick(View v) {
//handle user clicks here
EditText enteredText = (EditText) findViewById(R.id.enter);
String words = enteredText.getText().toString();
speakWords(words);
}
做
public void onClick(View v) {
//handle user clicks here
TextView myword = (TextView) findViewById(R.id.myword);
String words = myword.getText().toString();
speakWords(words);
}
答案 1 :(得分:-1)
这里这段代码适合我: 用户点击实现:
private void StartSpeak(final String data) {
TTS=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int initStatus) {
if (initStatus == TextToSpeech.SUCCESS) {
if(TTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
TTS.setLanguage(Locale.US);
TTS.setPitch(1.3f);
TTS.setSpeechRate(0.7f);
// start speak
speakWords(data);
}
else if (initStatus == TextToSpeech.ERROR) {
Toast.makeText(getApplicationContext(), "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
}
}
});
}
在说话前先启动方法:
private void speakWords(String speech) {
TTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
开始发送短信
{{1}}