2017-09-13 128 views
1

我正致力于语音识别,我需要用多种语言来完成此任务。多种语言的文本语音

什么,我到底要如果印地文或任何其他语言的用户对话,则需要显示它在文本视图。 现在它的英文作品完美无缺。

什么,我需要做的是多语言,每用户的选择..

请帮助我,谢谢你..

这里是我的代码

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; 
     } 
    } 
    } 
} 
+0

我不喜欢的东西,但我没有不力这是正确的方式或不? – pratikSonawane

回答

0

第1步。你需要根据语言选择 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,Locale.CHINESE)改变你的语言环境;

步骤2.保存语音文本转换成字符串文件。并使用getString方法。这样你可以根据语言使用字符串。

enter image description here

此代码可用于从不同的值取的文件夹不同的languge串..

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

我得到了语音文本字符串,但也需要按照用户语言打印,如果用户在中文讲话,然后在标签上打印也只需要中文。目前只有英文显示。 – pratikSonawane

+0

是的,你需要为不同的语言添加不同的价值观的文件夹为和使用的getString(R.string.speechText) –

+0

但在这里我需要设置在不同语言中的一些字符串中的每个文件夹吧? 我不想要这个。无论用户说什么我都需要打印那个。@ Harmindar Singh – pratikSonawane