2017-09-13 135 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