java—为什么文本到语音的状态是错误而不是成功实现?

kkih6yb8  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(126)

我正在尝试创建一个简单的文本到语音应用程序,但它抛出了一个错误,因此,当单击按钮时会出现toast,而不是将texttospeech.success作为int状态。
以下是我使用的代码:
包com.example.notifyme;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    EditText txt;
    Button b;
    String s;
    TextToSpeech tts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = findViewById(R.id.input);
        b = findViewById(R.id.button);
    }

    public void notify(View view) {//for creating tts activity
        tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                s = txt.getText().toString();
                if (status == TextToSpeech.SUCCESS) {
                    tts.setLanguage(Locale.ENGLISH);
                    tts.setSpeechRate(1.0f);
                    tts.speak(s, TextToSpeech.QUEUE_FLUSH, null, null);
                    Toast.makeText(getApplicationContext(),"Here it is",Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Something unexpected happened", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub

        if (tts != null) {

            tts.stop();
            tts.shutdown();
        }
        super.onPause();
    }

}

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/input"
        android:layout_width="330dp"
        android:layout_height="72dp"
        android:layout_marginTop="156dp"
        android:ems="10"
        android:hint="@string/add_your_text_here"
        android:gravity="start|top"
        android:layout_marginBottom="50dp"
        android:inputType="textMultiLine"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.493"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="114dp"
        android:layout_height="55dp"
        android:backgroundTint="#4CAF50"
        android:text="@string/speak"
        android:textAllCaps="true"
        android:onClick="notify"
        android:textFontWeight="40"
        app:layout_constraintTop_toBottomOf="@+id/input"
        android:layout_marginTop="30dp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="266dp" />
</androidx.constraintlayout.widget.ConstraintLayout/>

请告诉我是什么错误。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题