如何在android studio中将设置字符串转换为语音?

时间:2017-11-24 03:39:54

标签: java android text-to-speech

我正在尝试找出如何设置一个将被大声读出的字符串。

例如:

String text =“Hello”;

2 个答案:

答案 0 :(得分:2)

您可以找到一个非常详细的示例,说明如何在此site上将文字转换为语音。 HTH。

您的MainActivity.java应该看起来像这样

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
import android.widget.Toast;

public class MainActivity extends Activity {
   TextToSpeech t1;
   EditText ed1;
   Button b1;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ed1=(EditText)findViewById(R.id.editText);
      b1=(Button)findViewById(R.id.button);

      t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
         @Override
         public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
               t1.setLanguage(Locale.UK);
            }
         }
      });

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            String toSpeak = ed1.getText().toString();
            Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
            t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
         }
      });
   }

   public void onPause(){
      if(t1 !=null){
         t1.stop();
         t1.shutdown();
      }
      super.onPause();
   }
}

这是activity_main.xml的内容

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity"
   android:transitionGroup="true">

   <TextView android:text="Text to Speech" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />

   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:theme="@style/Base.TextAppearance.AppCompat" />

   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:layout_below="@+id/imageView"
      android:layout_marginTop="46dp"
      android:hint="Enter Text"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:textColor="#ff7aff10"
      android:textColorHint="#ffff23d1" />

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Text to Speech"
      android:id="@+id/button"
      android:layout_below="@+id/editText"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="46dp" />

</RelativeLayout>

答案 1 :(得分:2)

试试这个......

TextToSpeech textToSpeech;

的onCreate

textToSpeech = new TextToSpeech(this, this);

覆盖方法

要调用的回调指示TextToSpeech的完成    引擎初始化。

@see android.speech.tts.TextToSpeech.OnInitListener#onInit(int)

@Override
 public void onInit(int status) {
  if (status == TextToSpeech.SUCCESS) {
   int result = textToSpeech.setLanguage(Locale.US);
   if (result == TextToSpeech.LANG_MISSING_DATA
     || result == TextToSpeech.LANG_NOT_SUPPORTED) {
    Log.e("error", "This Language is not supported");
   } else {
    convertTextToSpeech();
   }
  } else {
   Log.e("error", "Initilization Failed!");
  }
 }

使用指定的排队策略和语音参数说出字符串。

private void convertTextToSpeech() {
  String text = inputText.getText().toString();

   text = "Hello World";

  textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
 }

选中此sample