안드로이드 음성 인식 기능 (STT)

프로그래밍/Android (Java)|2017. 10. 10. 17:37
반응형

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 :

  1. Start RecognizerIntent intent with the RecognizerIntent.ACTION_RECOGNIZE_SPEECH as action.
  2. Handle the text data returned by Speech to Text API

1. Manifest file

AndroidManifest.xml
<?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

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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:

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.

MainActivity.java
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 .

Speech to text demo
Speech to text demo

Offline mode is not enabled

Offline mode not enabled
Offline mode not enabled




[출처] http://stackandroid.com/tutorial/android-speech-to-text-tutorial/

반응형

댓글()