当我在 android 聊天机器人中将语音转换为文本作为输入时,我的文本到语音不起作用



我使用在线教程制作了一个聊天机器人,现在除了编写编辑文本作为输入外,我还使用语音识别。 但问题是当我按下语音识别按钮时 TTS 不起作用。我不知道我使用了各种方法的问题是什么。从编辑文本字段发送文本时,TTS 工作正常。 下面是主活动中两个代码的示例。第一个代码用于通过发送按钮发送文本,工作正常。第二个代码是我使用 stt 聊天并且 tts 不起作用的代码。需要帮助来解决问题。提前谢谢。

public class MainActivity extends AppCompatActivity {
private ListView mListView;
private FloatingActionButton mButtonSend;
private EditText mEditTextMessage;
private ImageView mImageView;
public Bot bot;
public static Chat chat;
private ChatMessage.ChatMessageAdapter mAdapter;
public Button buSpeak;
public TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR){
tts.setLanguage(Locale.US);
}
}
});
mListView = (ListView) findViewById(R.id.listView);
mButtonSend = (FloatingActionButton) findViewById(R.id.btn_send);
mEditTextMessage = (EditText) findViewById(R.id.et_message);
mImageView = (ImageView) findViewById(R.id.iv_image);
mAdapter = new ChatMessage.ChatMessageAdapter(this, new ArrayList<ChatMessage>());
mListView.setAdapter(mAdapter);
buSpeak = (Button)findViewById(R.id.buSpeak);
CheckUserPermsions();
//chat button
mButtonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = mEditTextMessage.getText().toString();
//bot
String response = chat.multisentenceRespond(mEditTextMessage.getText().toString());
if (TextUtils.isEmpty(message)) {
return;
}
sendMessage(message);
mimicOtherMessage(response);
mEditTextMessage.setText("");
mListView.setSelection(mAdapter.getCount() - 1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
tts.speak(response, TextToSpeech.QUEUE_FLUSH,null,null);
}else{
tts.speak(response, TextToSpeech.QUEUE_FLUSH,null);
}
}
});

以及使用语音识别的代码,这里的 tts 不起作用

public void buSpeak(View view) {
startVoiceRecognitionActivity();
}

private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//since you only want one, only request 1
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
startActivityForResult(intent, 1234);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK){
tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR){
tts.setLanguage(Locale.US);
}
}
});
//pull all of the matches
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String topResult = matches.get(0);
EditText AutoText = (EditText) findViewById(R.id.et_message);
AutoText.setText(topResult);
String message = AutoText.getText().toString();
//bot
String response = chat.multisentenceRespond(AutoText.getText().toString());
if (TextUtils.isEmpty(response)) {
return;
}
sendMessage(message);
mimicOtherMessage(response);
AutoText.setText("");
mListView.setSelection(mAdapter.getCount() - 1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
tts.speak(response, TextToSpeech.QUEUE_FLUSH,null,null);
}else{
tts.speak(response, TextToSpeech.QUEUE_FLUSH,null);
}
}
}
public void CheckUserPermsions(){
if ( Build.VERSION.SDK_INT >= 23){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED  ){
requestPermissions(new String[]{
android.Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_CODE_ASK_PERMISSIONS);
return ;
}
}
}
//get acces to location permsion
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
} else {
// Permission Denied
Toast.makeText( this,"denail" , Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
public void onPause(){
if(tts !=null){
tts.stop();
tts.shutdown();
}
super.onPause();
}
}

只需调用此方法并在此方法中传递文本

public void textToSpeech(String message){
if (result==TextToSpeech.LANG_NOT_SUPPORTED ||result==TextToSpeech.LANG_MISSING_DATA){
Toast.makeText(Activity.this, "Language is Not Supported", Toast.LENGTH_SHORT).show();
}else {
String text=message;
if (text==null||text.equals("")){
text="Text is Empty";
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null,null);
} else {
textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null);
}
}
}

好的,我已经解决了这个问题,而不是 onPause,只是在 tts 之后使用了 onDestry。 并添加了 tts.stop((;在 buSpeak 按钮方法中,以便在按下该按钮时停止 tts。

'下面的代码

public void buSpeak(View view( {

tts.stop();
startVoiceRecognitionActivity();
}
//after all other steps 
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}

最新更新