2014-11-28 9 views
0

Я разрабатываю приложение для Android, которое может распознавать речь на мандаринском языке, а затем выводить текст. Но я не могу найти, как это сделать. Может ли кто-нибудь дать мне пример кода распознавания речи на другом языке (мандарин, франция и т. Д.)?речь в текст api другой язык android

public class MainActivity extends Activity { 

private TextView txtSpeechInput; 
private ImageButton btnSpeak; 

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

    txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput); 
    btnSpeak = (ImageButton) findViewById(R.id.btnSpeak); 

    // hide the action bar 
    getActionBar().hide(); 

    btnSpeak.setOnClickListener(new View.OnClickListener() { 

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

} 



private static int SR_CODE = 123; 

/** 
* Initializes the speech recognizer and starts listening to the user input 
*/ 
private void listen() { 
     Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     //Specify language 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.SIMPLIFIED_CHINESE); 
     // Specify language model 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     // Specify how many results to receive 
     intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5); 
     // Start listening 
     startActivityForResult(intent, SR_CODE); 
    } 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == SR_CODE && resultCode == RESULT_OK) { 
     if (data != null) { 
      // Retrieves the best list SR result 
      ArrayList<String> nBestList = data 
        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      String bestResult = nBestList.get(0); 
      Toast.makeText(getApplicationContext(), bestResult, 
        Toast.LENGTH_LONG).show(); 
     } else { 
      // Reports error in recognition error in log 
      Log.e("Log", "Recognition was not successful"); 
     } 
    } 

} 



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
} 
+0

ли решение при условии работы? –

+0

Извините, я только что открыл этот вопрос. Это работает как мой пост внизу. Я не знаю, почему {locale.SIMPLEFIED_CHINESE} не может дать китайское слово. Но спасибо, что помогли мне. –

ответ

1

Вы можете установить RecognizerIntent.EXTRA_LANGUAGE при вызове распознавателя.

Так пример на упрощенном китайском будет:

private static int SR_CODE = 123; 


/** 
    * Initializes the speech recognizer and starts listening to the user input 
    */ 
    private void listen() { 
     Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     //Specify language 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.SIMPLIFIED_CHINESE) 
     // Specify language model 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     // Specify how many results to receive 
     intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5); 
     // Start listening 
     startActivityForResult(intent, SR_CODE); 
    } 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == SR_CODE && resultCode == RESULT_OK) { 
       if(data!=null) { 
       //Retrieves the best list SR result 
       ArrayList<String> nBestList = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
       String bestResult = nBestList.get(0); 
       Toast.makeText(getApplicationContext(), bestResult, Toast.LENGTH_LONG).show;    
      }else {   
       //Reports error in recognition error in log 
       Log.e(LOGTAG, "Recognition was not successful"); 
      } 

    } 
+0

что такое 'SR_CODE'? –

+0

Это целочисленная константа, используемая в обратном вызове onActivityResult. Я обновил свой ответ. Дайте мне знать, если проблема не решена. –

+0

Я сделал ваш код. но он всегда отображается на английском языке. Я обновил свой код вопроса, как ваш код. Но большое спасибо за помощь –

2
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "zh"); 
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "zh"); 
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "zh"); 
intent.putExtra(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES, "zh"); 
intent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE,"zh"); 
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "zh"); 
intent.putExtra(RecognizerIntent.EXTRA_RESULTS, "zh");