首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多语言语音转文本

多语言语音转文本
EN

Stack Overflow用户
提问于 2017-09-13 18:12:02
回答 3查看 2.2K关注 0票数 1

我正在从事语音识别工作,我需要用多种语言来做这件事。

我到底想要什么,如果用户在印地语或任何其他语言说话,然后需要在文本视图上显示它。现在,它完美的工作在英语上。

根据用户的选择,我需要为多种语言执行此操作..

请帮帮我,谢谢..

以下是我的代码

代码语言:javascript
复制
 public class MainActivity extends AppCompatActivity {

private static final int REQ_CODE_SPEECH_INPUT = 100;
private TextView mVoiceInputTv;
private ImageButton mSpeakBtn;
private TextToSpeech t1;

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

    mVoiceInputTv = (TextView) findViewById(R.id.voiceInput);
    mSpeakBtn = (ImageButton) findViewById(R.id.btnSpeak);
    mSpeakBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startVoiceInput();
        }
    });
}

private void startVoiceInput() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hello, How can I help you?");
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    }
    catch (ActivityNotFoundException a) {

    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == RESULT_OK && null != data) {
                ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                if(userlanguage.equals("English")){
                    mVoiceInputTv.setText(result.get(0));
                }
                else if(userlanguage.equals("Hindi")){
                    face = Typeface.createFromAsset(getAssets(),"fonts/DroidHindi.ttf");
                    mVoiceInputTv.setTypeface(face);
                    mVoiceInputTv.setText(result.get(0));
                }
                else if(userlanguage.equals("Marathi")){
                    face = Typeface.createFromAsset(getAssets(),"fonts/Marathi.ttf");
                    mVoiceInputTv.setTypeface(face);
                    mVoiceInputTv.setText(result.get(0));
                }
                else if(userlanguage.equals("Gujarati")){
                    face = Typeface.createFromAsset(getAssets(),"fonts/Gujarati.ttf");
                    mVoiceInputTv.setTypeface(face);
                    mVoiceInputTv.setText(result.get(0));
                }
            }
            break;
        }
    }
  }
}
EN

回答 3

Stack Overflow用户

发布于 2017-09-13 18:20:44

步骤1.您需要根据language selection intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,Locale.CHINESE);更改语言环境

步骤2.将语音文本保存到字符串文件中。并使用getString方法。这样你就可以根据语言来使用字符串了。

此代码可用于从不同的值文件夹中获取不同的语言字符串。

代码语言:javascript
复制
    Configuration conf = getResources().getConfiguration();
    conf.locale = new Locale("pl");
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    Resources resources = new Resources(getAssets(), metrics, conf);
    String str = resources.getString(id);
票数 0
EN

Stack Overflow用户

发布于 2017-09-13 18:21:51

谷歌的服务器目前支持英语、普通话和日语。web搜索模型在所有三种语言中都可用,而自由格式主要针对英语进行了优化。

设置EXTRA_LANGUAGE的值。请参阅http://developer.android.com/reference/android/speech/RecognizerIntent.html#EXTRA_LANGUAGE

票数 0
EN

Stack Overflow用户

发布于 2019-04-27 11:25:17

您将为不同的语言创建不同的活动...

例如:假设您选择了印地语,现在它将转到印地语活动。就像这样……

代码:

代码语言:javascript
复制
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");

另一项活动:

代码语言:javascript
复制
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "hi");

我希望这能行得通。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46194774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档