2010년 12월 8일 수요일

[Android] 음성 인식 API 소스

 

 

 

 

구글링중에 음성인식 API를 사용해서 음성인식 하는 방법을 찾았다.

나름 인식률 80프로 이상 인거 같다..

인식률이 잘나오는 만큼 다방면에 사용될거 같은..

음성 입력후 패턴분석된 결과 리스트가 나열되는 소스이다.

 

 

voiceDemo.java 파일

 

 

public class voiceDemo extends Activity implements OnClickListener {
 private static final int REQUEST_CODE = 1234;
 private ListView mList;
 private ListView mDebug;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Button speakButton = (Button) findViewById(R.id.btn_speak);
  speakButton.setText("음성입력");
  mList = (ListView) findViewById(R.id.list);
  PackageManager pm = getPackageManager();
  List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
    RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
  if (activities.size() != 0) {
   speakButton.setOnClickListener(this);
  } else {
   speakButton.setEnabled(false);
   speakButton.setText("Recognizer not Present");
  }
 }

 public void onClick(View v) {
  if (v.getId() == R.id.btn_speak) {
   startVoiceRecognitionActivity();
  }
 }

 private void startVoiceRecognitionActivity() {
  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,
     "Free Form Language Model Demo");
   startActivityForResult(intent, REQUEST_CODE);
  } catch (ActivityNotFoundException ex) {
   Toast
     .makeText(voiceDemo.this, "Activity Not Found",
       Toast.LENGTH_LONG).show();
  }
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
   ArrayList<String> matches = data
     .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
   mList.setAdapter(new ArrayAdapter<String>(this,
     android.R.layout.simple_list_item_1, matches));
  }
  super.onActivityResult(requestCode, resultCode, data);
 }
}

 

 

 

 

 

 

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">
 <TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:paddingBottom="4dip" />
 <Button
 android:id="@+id/btn_speak"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/speak_button" />
 <ListView
 android:id="@+id/list"
 android:layout_width="fill_parent"
 android:layout_height="0dip" android:layout_weight="1" />
</LinearLayout>

 

 

 

댓글 없음:

댓글 쓰기