繁体   English   中英

如何为印地语添加文本到语音?

[英]How to add Text to speech for Hindi language?

我的应用程序由两个字符串和两个按钮组成,一个英文字符串和印地文字符串,当我单击英语朗读按钮时,英文字符串正在发音,当我来到印地文字符串时,它没有响应提到的给定单词

这是我的MainActivity的样子,

public class MainActivity extends AppCompatActivity {

private TextView englishString, hindiString;
private Button englishButton, hindiButton;
private TextToSpeech textToSpeech;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    englishString = (TextView) findViewById(R.id.englishString);
    hindiString = (TextView) findViewById(R.id.hindiString);

    englishButton = (Button) findViewById(R.id.englishButton);
    hindiButton = (Button) findViewById(R.id.hindiButton);

    englishButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            loadSpeakingLanguages(englishString.getText().toString());
        }
    });

    hindiButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            loadSpeakingLanguages(hindiString.getText().toString());
        }
    });
}


private void loadSpeakingLanguages(String textToTranslate) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ttsGreater21(textToTranslate);
    } else {
        ttsUnder20(textToTranslate);
    }
}

@SuppressWarnings("deprecation")
private void ttsUnder20(String text) {
    HashMap<String, String> map = new HashMap<>();
    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
    textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, map);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void ttsGreater21(String text) {
    String utteranceId = this.hashCode() + "";
    textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);
}


@Override
protected void onResume() {
    textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status != TextToSpeech.ERROR) {
                textToSpeech.setLanguage(Locale.ENGLISH);
            }
        }
    });
    super.onResume();
}

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

这是我的activity_main的样子,

<?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: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="ravi.texttospeech.MainActivity">

<TextView
    android:id="@+id/englishString"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textSize="25dp" />

<TextView
    android:id="@+id/hindiString"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/englishString"
    android:text="नमस्ते दुनिया"
    android:textSize="25dp" />

<Button
    android:id="@+id/englishButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/hindiString"
    android:text="Speak English Text" />

<Button
    android:id="@+id/hindiButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/englishButton"
    android:text="Speak Hindi Text" />

这是我的应用程序的样子

我已经对您的代码进行了一些修改

 hindiButton.setOnClickListener(new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onClick(View view) {
            loadSpeakingLanguages(hindiString.getText().toString());
            textToSpeech.setLanguage(Locale.forLanguageTag("hin"));
        }
    });

它对我来说很好。

您需要使用ISO 639 3个字母的单词或ISO 639 2个字母的单词

查看ISO国家名称以及链接中的 相应代码

注意:此方法仅在棒棒糖和下一版本中有效

setLanguage(new Locale("hi","IN"))

尝试这个:-

Locale locale = new Locale("hi", "IN");
            int avail = textToSpeech.isLanguageAvailable(locale);
            switch (avail) {
                case TextToSpeech.LANG_AVAILABLE:
                    textToSpeech.setLanguage(Locale.forLanguageTag("hi"));
                    isLanguageAvailable = true;
                    break;
                case TextToSpeech.LANG_COUNTRY_AVAILABLE:
                case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
                    textToSpeech.setLanguage(locale);
                    isLanguageAvailable = true;
                    break;
            }

将默认引擎更改为com.google.android.tts对我有用。

例子:

new TextToSpeech(
                            context.getApplicationContext(),
                            new TextToSpeech.OnInitListener() {
                                @Override
                                public void onInit(int status) {
                                    if (status != TextToSpeech.ERROR) {
                                        if (textToSpeech != null) {
                                            textToSpeech.setLanguage(new Locale("en", "IN"));
                                            textToSpeech.setSpeechRate(1);
                                        }
                                    }
                                }
                            },
                            "com.google.android.tts");

添加此导入:

import android.speech.tts.TextToSpeech

实现这个监听器

class DetailDay : AppCompatActivity(), TextToSpeech.OnInitListener  {

然后覆盖(粘贴)此方法:

override fun onInit(intVar: Int) {
        if(intVar==TextToSpeech.SUCCESS){
            var local= Locale("hi","IN")
            val resultado=tts!!.setLanguage(local)
            if(resultado==TextToSpeech.LANG_MISSING_DATA){
                Log.i(TAG,"lang not found")
            }
        }
    }

然后创建这个变量:

 private var tts:TextToSpeech?=null

在 onCreate 中初始化(推荐)

tts= TextToSpeech(this,this)

要开始播放,请调用此方法:

 tts!!.speak(stringData, TextToSpeech.QUEUE_FLUSH, null);

要停止播放,

  tts!!.stop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM