2011.02.08 작성 / 2011.03.22 갱신(내용수정 없음)
음성인식은 간단하게 처리 할 수 있습니다.
1. 인텐트를 만들고
2. startActivityForResult 로 액티비티를 시작하고
3. onActivityResult 메소드를 override해서 음성인식 결과를 처리하면 됩니다.
인텐트 엑스트라(Intent extra)의 값들
public void find_voice()
{
// 인텐트를 만들고 액티비티를 시작한다.
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "주소록 음성 검색");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
startActivityForResult(intent, 17);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 17 && resultCode == RESULT_OK)
{
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
// TODO Do something with the recognized voice strings
// results에 음성인식 결과들이 들어가 있다.
// 이 부분에서 그 결과들로 어떤 처리를 할지 코딩하면 된다.
}
super.onActivityResult(requestCode, resultCode, data);
} |
requestCode는 숫자를 직접 지정해서 넘기지 않아도 된다. 상수를 사용해도 됩니다.
저는 명시적으로 하기 위해 숫자를 써두었을 뿐입니다.
인텐트 엑스트라(Intent extra)의 값들
RecognizerIntent.EXTRA_LANGUAGE_MODEL
: 음성인식의 startActivity의 intent는 반드시 이 값을 가져야 합니다.
RecognizerIntent에 정의되 있는 static 상수 중 하나를 사용하면 됩니다.
(
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM /
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH )
RecognizerIntent.EXTRA_PROMPT
: 위의 음성인식 다이얼로그에 보이는 "주소록 음성 검색"위치에 들어갈 스트링입니다.
RecognizerIntent.EXTRA_MAX_RESULTS
: 잠재적 인식결과(potential recognition results)의 수를 제한한다. 라고 되있습니다.
몇개의 결과를 보여줄지 라고 생각하면 될듯합니다.
RecognizerIntent.EXTRA_LANGUAGE
: 인식할 언어가 Device의 기본값과 다를 경우 지정합니다.
Locale 클래스에 정의된 언어 상수들 중 하나를 지정하면 됩니다.
현재 설정된 기본 값은 Locale.getDefault() 메소드 호출로 알 수 있습니다.
'Android' 카테고리의 다른 글
[Android] File Path from Uri (Uri를 통해 파일경로 구하기) (0) | 2011.04.26 |
---|---|
[Android] App Widget - 앱 위젯 (6) | 2011.04.01 |
[Android] EditText - 에딧텍스트 : 숫자키보드, 숫자만 입력받기 (0) | 2011.03.22 |
[Android] 작은 이미지 파일로 큰 배경 만들기 - 9.patch (NINE PATCH) (0) | 2011.02.14 |
[Andoird] Logging을 위한 TAG 만들어두기 (0) | 2011.02.11 |