您的位置:首页 > 移动开发 > Android开发

.android 语音(声音)转文字

2013-01-26 20:43 337 查看
public class SoundTestActivity extends Activity {

/** Called when the activity is first created. */

private static final int RECOGNIZER_EXAMPLE=101;

private TextView result;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

result=(TextView)findViewById(R.id.text_result);

Button start=(Button)findViewById(R.id.start_button);

start.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say a word or phrase\nand it will show as text");

startActivityForResult(intent,RECOGNIZER_EXAMPLE);

}

});

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// TODO Auto-generated method stub

if(requestCode==RECOGNIZER_EXAMPLE&&resultCode==RESULT_OK){

ArrayList<String> resultList=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

StringBuffer resultListString=new StringBuffer();;

for(String s:resultList){

resultListString.append(s+",");

}

result.setText(resultListString.toString());

}

super.onActivityResult(requestCode, resultCode, data);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: