티스토리 뷰
// main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id = "@+id/EdtName"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
/>
<Button
android:id = "@+id/BtnSpeech"
android:layout_width = "fill_parent"
android:layout_height = "50px"
android:text = "음성 인식"
></Button>
</LinearLayout>
// SpeechToTextActivity.java
package pkg.SpeechToText;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SpeechToTextActivity extends Activity {
private static final int REQUEST_CODE = 0;
private EditText mEdit;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEdit = (EditText)findViewById(R.id.EdtName);
findViewById(R.id.BtnSpeech).setOnClickListener(mClick);
}
Button.OnClickListener mClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.BtnSpeech :
try {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "SpeechToTextActivity");
startActivityForResult(intent, REQUEST_CODE);
} catch(ActivityNotFoundException e){
Toast.makeText(this, "음성 인식 실 패 !!", Toast.LENGTH_SHORT).show();
}
break;
}
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String str = "";
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
/*
for(int i = 0; i < results.size(); i ++){
str += results.get(i);
}
*/
str = results.get(0);
mEdit.setText(str);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
- Total
- Today
- Yesterday