【问题标题】:How can I get TTS to say in different languages in an app?如何让 TTS 在应用程序中用不同的语言说?
【发布时间】:2016-07-07 20:09:29
【问题描述】:

我是初学者,这是我的第一个应用程序。即使在使用 setLanguage() 之后,我也无法让 tts 说不同的语言。它总是用英语说话。任何帮助,将不胜感激。这是我的 MainActivity:

package com.example.android.sayhi;

import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    TextToSpeech t;
    String option;
    Locale language;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Spinner dropdown = (Spinner)findViewById(R.id.spinner);
        String[] items = new String[]{Locale.FRENCH.getDisplayLanguage(),Locale.UK.getDisplayLanguage(), Locale.US.getDisplayLanguage()};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
        dropdown.setAdapter(adapter);

      dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
              option =  adapterView.getItemAtPosition(i).toString();


          }

          @Override
          public void onNothingSelected(AdapterView<?> adapterView) {
option = adapterView.getItemAtPosition(0).toString();
          }


      });
        language = new Locale(option);
        t = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    t.setLanguage(language);
                    t.setSpeechRate((float)0.8);


                }
            }
        });




    }


    public void show(View view) {
        EditText textField = (EditText) findViewById(R.id.text);
        String field = textField.getText().toString();


        TextView viewField = (TextView) findViewById(R.id.textView);
        viewField.setText(field);

        if (Build.VERSION.RELEASE.startsWith("5")) {
          t.speak(field, TextToSpeech.QUEUE_FLUSH, null, null);
        }
        else {
            t.speak(field, TextToSpeech.QUEUE_FLUSH, null);
        }

    }

    public void onPause() {
        if (t != null) {
            t.stop();

        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

这是我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView

    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">


<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.sayhi.MainActivity">


    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:width="300dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Say it!"
        android:id="@+id/button"
        android:layout_below="@+id/text"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="38dp"
        android:onClick="show"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Write it up!"
        android:id="@+id/textView"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="105dp"
        android:gravity="center_horizontal" />

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/spinner"
        android:layout_below="@+id/button"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="@android:drawable/btn_dropdown"/>

</RelativeLayout>
</ScrollView>

任何帮助将不胜感激!谢谢。

【问题讨论】:

    标签: java android android-studio locale text-to-speech


    【解决方案1】:

    您正在 onCreate() 方法中实例化 Text to Speech 对象并尝试在 onInit() 中设置语言。

    此时您的变量option 将为空(因为尚未选择任何内容),因此使用空值创建区域设置将失败,因此不会设置语言。

    当从下拉列表中选择项目时,option 会被填充,但不会发生任何进一步的事情。它不会在侦听器的“外部”继续返回 onCreate() 方法。

    在您的下拉侦听器中,添加以下内容:

        option =  adapterView.getItemAtPosition(xxx).toString();
        language = new Locale(option);
    
        if (t != null) {
            int result = t.setLanguage(language)
            // Check the result to see if it was successful
        } else {
            // the tts object was null?
        }
    

    这应该可以解决问题。

    另外,由于您将使用上述代码两次,因此最好为它创建自己的方法。

    private void setLanguage(final String option) {
    
        language = new Locale(option);
    
        if (t != null) {
        int result = t.setLanguage(language)
        // Check the result to see if it was successful
        } else {
        // the tts object was null?
        }
    
    }
    

    那么你可以简单地调用:

    setLanguage(adapterView.getItemAtPosition(xxx).toString());
    

    每当您创建变量和赋值时,作为初学者,请始终记录该值,以便您自己查看。如果它为 null 或不是您想要的值,它将为您提供早期线索。

    【讨论】:

    • 工作正常。谢谢。但是 TTS 初始化有点慢,可能和我设备的资源消耗有关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多