语音到文本api其他语言的android [英] speech to text api other language android
问题描述
我开发的android应用程序可以识别普通话的语音,然后生成文本。但我找不到如何做到这一点。有人可以给我例如语言识别代码在其他语言(普通话,法国等)吗?
公共类MainActivity延伸活动{
私人TextView txtSpeechInput;
私人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);
//隐藏操作栏
getActionBar()。hide();
btnSpeak.setOnClickListener(new View.OnClickListener(){
$ b @Override
public void onClick(View v){
listen();
}
});
}
private static int SR_CODE = 123;
$ b / **
*初始化语音识别器并开始监听用户输入
* /
private void listen(){
意图意图=新意图(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//指定语言
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,Locale.SIMPLIFIED_CHINESE);
//指定语言模型
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//指定接收多少个结果
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
//开始监听
startActivityForResult(intent,SR_CODE);
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode == SR_CODE&& resultCode == RESULT_OK){
if(data!= null){
//检索最佳列表SR结果
ArrayList< String> nBestList = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String bestResult = nBestList.get(0);
Toast.makeText(getApplicationContext(),bestResult,
Toast.LENGTH_LONG).show();
} else {
//在记录
中报告识别错误中的错误Log.e(Log,Recognition was not successful);
@Override
public boolean onCreateOptionsMenu(Menu menu){
//膨胀菜单;这会将项目添加到操作栏(如果存在)。
getMenuInflater()。inflate(R.menu.main,menu);
返回true;
$ div $解析方案
你您可以在致电识别器时设置 RecognizerIntent.EXTRA_LANGUAGE 。
所以一个简体中文的例子是:
pre $ private static int SR_CODE = 123;
$ b / **
*初始化语音识别器并开始监听用户输入
* /
private void listen(){
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//指定语言
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,Locale.SIMPLIFIED_CHINESE)
//指定语言模型
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//指定接收多少个结果
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
//开始监听
startActivityForResult(intent,SR_CODE);
$ b @Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode == SR_CODE&& amp; ; resultCode == RESULT_OK){
if(data!= null){
//检索最佳列表SR结果
ArrayList< String> nBestList = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String bestResult = nBestList.get(0);
Toast.makeText(getApplicationContext(),bestResult,Toast.LENGTH_LONG).show;
} else {
//在log
Log.e(LOGTAG,识别失败)中报告识别错误中的错误;
}
}
I develop android application that can recognize speech in Mandarin, then yield text. But i can't find how to do that. Can someone give me example code of speech recognition in other language(mandarin, france, etc) ?
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;
}
}
You can set RecognizerIntent.EXTRA_LANGUAGE when you call the recognizer.
So an example in simplified chinese will be:
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");
}
}
这篇关于语音到文本api其他语言的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!