Android
음성 변환 - 텍스트 변환
수색…
기본 Google 음성 안내 대화 상자로 텍스트 음성
음성을 텍스트 번역으로 트리거
private void startListening() {
//Intent to listen to user vocal input and return result in same activity
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//Use a language model based on free-form speech recognition.
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
//Message to display in dialog box
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_to_text_info));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
onActivityResult에서 번역 결과 가져 오기
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
산출
대화없이 텍스트로 말하기
다음 코드는 대화 상자를 표시하지 않고 음성 대 텍스트 번역을 시작하는 데 사용할 수 있습니다.
public void startListeningWithoutDialog() {
// Intent to listen to user vocal input and return the result to the same activity.
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// Use a language model based on free-form speech recognition.
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
appContext.getPackageName());
// Add custom listeners.
CustomRecognitionListener listener = new CustomRecognitionListener();
SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(appContext);
sr.setRecognitionListener(listener);
sr.startListening(intent);
}
위의 코드에서 사용 된 사용자 지정 수신기 클래스 CustomRecognitionListener
는 다음과 같이 구현됩니다.
class CustomRecognitionListener implements RecognitionListener {
private static final String TAG = "RecognitionListener";
public void onReadyForSpeech(Bundle params) {
Log.d(TAG, "onReadyForSpeech");
}
public void onBeginningOfSpeech() {
Log.d(TAG, "onBeginningOfSpeech");
}
public void onRmsChanged(float rmsdB) {
Log.d(TAG, "onRmsChanged");
}
public void onBufferReceived(byte[] buffer) {
Log.d(TAG, "onBufferReceived");
}
public void onEndOfSpeech() {
Log.d(TAG, "onEndofSpeech");
}
public void onError(int error) {
Log.e(TAG, "error " + error);
conversionCallaback.onErrorOccured(TranslatorUtil.getErrorText(error));
}
public void onResults(Bundle results) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
}
public void onPartialResults(Bundle partialResults) {
Log.d(TAG, "onPartialResults");
}
public void onEvent(int eventType, Bundle params) {
Log.d(TAG, "onEvent " + eventType);
}
}
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow