首页 > 解决方案 > 如果我运行一次语音到文本,那么文本到语音不起作用,只显示吐司

问题描述

代码很好,但是当我点击语音到文本时,文本到语音不起作用

这是java文件

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.Locale;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class icanhearandspeak extends Activity {
TextToSpeech t2;
EditText ed2;
Button b2;
protected static final int RESULT_SPEECH = 1;
private ImageButton btnSpeak1;
private TextView txtText1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.xyxyxyxyxyx);
    ed2 = (EditText) findViewById(R.id.editText1);
    b2 = (Button) findViewById(R.id.button5);
    txtText1 = (TextView) findViewById(R.id.txtText1);
    btnSpeak1 = (ImageButton) findViewById(R.id.btnSpeak1);
    t2 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status != TextToSpeech.SUCCESS) {
                t2.setLanguage(Locale.UK);
            }
        }
    });
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak = ed2.getText().toString();
            Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
            t2.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
        }
    });
    btnSpeak1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(
                    RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
            try {
                startActivityForResult(intent, RESULT_SPEECH);
                txtText1.setText("");
            } catch (ActivityNotFoundException a) {
                Toast t = Toast.makeText(getApplicationContext(),
                        "Opps! Your device doesn't support Speech to Text",
                        Toast.LENGTH_SHORT);
                t.show();
            }
        }
    });
}
public void onPause() {
    if (t2 != null) {
        t2.stop();
        t2.shutdown();
    }
    super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case RESULT_SPEECH: {
            if (resultCode == RESULT_OK && null != data) {
                ArrayList<String> text = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                txtText1.setText(text.get(0));
            }
            break;
        }

    }
}

}

这是我的 xml 文件,它有一个按钮,可以收听音频并将其转换为文本和编辑文本,无论用户写什么都将转换为语音

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/jjjjj"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:orientation="vertical"
tools:context=".xyxyxyxyxyxyxyx">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight=".3">
<TextView android:text="xyxyxyxy"
    android:textColor="#003366"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:id="@+id/textview11"
    android:textSize="36dp"
    android:layout_weight="0.04" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="xyxyxyxy"
    android:id="@+id/textView1"
    android:layout_below="@+id/textview11"
    android:layout_centerHorizontal="true"
    android:textColor="#000080"
    android:textSize="34dp"
    android:layout_gravity="center_horizontal" />
<ImageButton
    android:id="@+id/btnSpeak1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/speak"
    android:src="@android:drawable/ic_btn_speak_now"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true" />
    <TextView
        android:id="@+id/txtText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/btnSpeak1"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight=".6">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="xyxyxyxyxy"
        android:layout_above="@+id/editText"
        android:layout_centerHorizontal="true"
        android:textColor="#000080"
        android:textSize="34dp"
        android:layout_gravity="center_horizontal"
        android:id="@+id/textView4" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText1"
        android:hint="Enter Text"
        android:textColor="#000080"
        android:textColorHint="#8470ff"
        android:layout_below="@+id/textView4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text to Speech"
        android:id="@+id/button5"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>

代码运行流畅,但是一旦我将语音转换为文本并尝试将文本转换为语音,它将无法正常工作,它只会显示吐司,但文本不会转换为语音

标签: javaandroidtext-to-speechspeech-to-text

解决方案


推荐阅读