안드로이드 음성 인식 기능 (STT)
In this tutorial we are going to show how to use Android’s Speech To Text API. Android currently supports offline mode also. The only thing is that you have to downlad offline language packages.
To learn how to enable offline mode speech to text, please follow the tutorial How to enable offline Speech To Text in Android
See following Steps :
- Start RecognizerIntent intent with the RecognizerIntent.ACTION_RECOGNIZE_SPEECH as action.
- Handle the text data returned by Speech to Text API
1. Manifest file
<? xml version = "1.0" encoding = "utf-8" ?> package = "com.stackandroid.speechtotext" android:versionCode = "1" android:versionName = "1.0" > < uses-sdk android:minSdkVersion = "19" android:targetSdkVersion = "21" /> < uses-permission android:name = "android.permission.INTERNET" /> < application android:allowBackup = "true" android:icon = "@drawable/ic_launcher" android:label = "@string/app_name" android:theme = "@style/AppTheme" > < activity android:name = "MainActivity" android:label = "@string/app_name" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > </ application > </ manifest > |
2. Activity layout file
android:layout_width = "match_parent" android:layout_height = "match_parent" android:background = "@color/bg_color" android:orientation = "vertical" > < TextView android:id = "@+id/txt_output" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignParentTop = "true" android:layout_centerHorizontal = "true" android:layout_marginTop = "100dp" android:textColor = "@color/white" android:textSize = "26dp" android:textStyle = "normal" /> < LinearLayout android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignParentBottom = "true" android:layout_centerHorizontal = "true" android:layout_marginBottom = "60dp" android:gravity = "center" android:orientation = "vertical" > < ImageButton android:id = "@+id/btn_mic" android:layout_width = "80dp" android:layout_height = "80dp" android:background = "@null" android:scaleType = "centerCrop" android:src = "@drawable/microphone" /> < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginTop = "10dp" android:text = "Speech to text using Google API" android:textColor = "@color/white" android:textSize = "15dp" android:textStyle = "normal" /> </ LinearLayout > </ RelativeLayout > |
3. Main Activity code
Start the speech recognizer intent using startActivityForResult() with bundle extras.
Required extras:
Optional extras:
EXTRA_PROMPT
EXTRA_LANGUAGE
EXTRA_MAX_RESULTS
EXTRA_RESULTS_PENDINGINTENT
EXTRA_RESULTS_PENDINGINTENT_BUNDLE
Result extras (returned in the result, not to be specified in the request):
Note: The extra EXTRA_LANGUAGE_MODEL is mandatory. Also handle ActivityNotFoundException there may be no applications installed in the device to support this speech recognition action.
package com.stackandroid.speechtotext; import java.util.ArrayList; import java.util.Locale; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.os.Bundle; import android.speech.RecognizerIntent; import android.view.View; import android.widget.ImageButton; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private final int SPEECH_RECOGNITION_CODE = 1 ; private TextView txtOutput; private ImageButton btnMicrophone; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtOutput = (TextView) findViewById(R.id.txt_output); btnMicrophone = (ImageButton) findViewById(R.id.btn_mic); btnMicrophone.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { startSpeechToText(); } }); } /** * Start speech to text intent. This opens up Google Speech Recognition API dialog box to listen the speech input. * */ private void startSpeechToText() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak something..." ); try { startActivityForResult(intent, SPEECH_RECOGNITION_CODE); } catch (ActivityNotFoundException a) { Toast.makeText(getApplicationContext(), "Sorry! Speech recognition is not supported in this device." , Toast.LENGTH_SHORT).show(); } } /** * Callback for speech recognition activity * */ @Override protected void onActivityResult( int requestCode, int resultCode, Intent data) { super .onActivityResult(requestCode, resultCode, data); switch (requestCode) { case SPEECH_RECOGNITION_CODE: { if (resultCode == RESULT_OK && null != data) { ArrayList<String> result = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); String text = result.get( 0 ); txtOutput.setText(text); } break ; } } } } |
4. Demonstration
Speech to text demonstration using RecognizerIntent .
Offline mode is not enabled
[출처] http://stackandroid.com/tutorial/android-speech-to-text-tutorial/
'프로그래밍 > Android (Java)' 카테고리의 다른 글
EditText 입력값 다른 Activity 에서 출력하기 (0) | 2018.04.09 |
---|---|
Android 개발환경 구성 (ver 2018-03-21) (0) | 2018.03.21 |
웹서버를 통한 파일 자동 업데이트 (0) | 2017.09.27 |
네비게이션바 (navigation bar) 상태 확인 (0) | 2017.09.14 |
웹페이지 텍스트 내용 가져와서 출력하기 (0) | 2017.05.22 |