Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Tích hợp Speech-to-Text (Nhận diện giọng nói) trong Android

Tổng quan

Android cung cấp API nhận diện giọng nói qua RecognizerIntent.ACTION_RECOGNIZE_SPEECH. Yêu cầu thiết bị có Google App/Assistant.

Các bước

Bước 1: Tạo project

Bước 2: Layout activity_main.xml

<RelativeLayout ...>
    <LinearLayout android:layout_width="match_parent" android:gravity="center" android:layout_height="match_parent">
        <TextView android:id="@+id/text" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerInParent="true" android:orientation="vertical">
        <ImageView android:id="@+id/speak" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="?selectableItemBackground" android:src="@android:drawable/ic_btn_speak_now" />
    </LinearLayout>
</RelativeLayout>

Bước 3: MainActivity.java

public class MainActivity extends AppCompatActivity {
    private static final int REQ_CODE = 100;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text);
        findViewById(R.id.speak).setOnClickListener(v -> startSpeechRecognition());
    }

    private void startSpeechRecognition() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Need to speak");
        try {
            startActivityForResult(intent, REQ_CODE);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(this, "Device not supported", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQ_CODE && resultCode == RESULT_OK && data != null) {
            ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            if (result != null && !result.isEmpty()) {
                textView.setText(result.get(0)); // Lấy kết quả đầu tiên
            }
        }
    }
}

Giải thích:

  • LANGUAGE_MODEL_FREE_FORM: Nhận diện tự do (dictation).
  • EXTRA_LANGUAGE: Ngôn ngữ (mặc định locale thiết bị).
  • EXTRA_PROMPT: Text gợi ý trên dialog nhận diện.
  • Kết quả trả về trong onActivityResult dạng ArrayList, lấy index 0.

Kết quả

Màn hình chính:

Tích hợp Speech-to-Text (Nhận diện giọng nói) trong Android

Click mic → Dialog Google Speech:

Tích hợp Speech-to-Text (Nhận diện giọng nói) trong Android

Nói "Hey GOOGLE" → Kết quả hiển thị:

Tích hợp Speech-to-Text (Nhận diện giọng nói) trong Android

Lưu ý

  • Cần kết nối internet.
  • Thiết bị phải có Google App.
  • Từ API 23 cần quyền RECORD_AUDIO runtime.