Nhập bằng giọng nói

Mọi thiết bị Wear OS đều có micrô nên người dùng có thể tương tác với thiết bị qua giọng nói. Bạn có thể chia các tương tác này thành ba loại:

  • Ghi âm
  • Nhận dữ liệu nhập bằng lời nói tuỳ ý
  • Thao tác bằng giọng nói

Ghi âm

Tính năng ghi âm trên thiết bị Wear OS hoạt động giống như trên điện thoại. Hãy tham khảo tài liệu MediaRecorder để tìm hiểu thêm về cách ghi âm trên Android. Bạn cũng có thể xem ví dụ về cách triển khai trong ví dụ về Wear Speaker trên GitHub.

Nhận dữ liệu nhập bằng lời nói tuỳ ý

Gọi hoạt động Nhận dạng lời nói tích hợp của hệ thống để nhận dữ liệu nhập bằng lời nói từ người dùng. Sử dụng tính năng nhập liệu bằng lời nói để gửi tin nhắn hoặc thực hiện thao tác tìm kiếm.

Trong ứng dụng của bạn, hãy gọi startActivityForResult() bằng thao tác ACTION_RECOGNIZE_SPEECH. Thao tác này sẽ bắt đầu hoạt động nhận dạng lời nói, sau đó bạn có thể xử lý kết quả trong onActivityResult().

Mã mẫu sau đây cho biết cách bắt đầu và xử lý một hoạt động nhận dạng lời nói.

Kotlin

private const val SPEECH_REQUEST_CODE = 0
...
// Create an intent that can start the Speech Recognizer activity
private fun displaySpeechRecognizer() {
    val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
        putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
    }
    // This starts the activity and populates the intent with the speech text.
    startActivityForResult(intent, SPEECH_REQUEST_CODE)
}

// This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    if (requestCode == SPEECH_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        val spokenText: String? =
                data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS).let { results ->
                    results[0]
                }
        // Do something with spokenText.
    }
    super.onActivityResult(requestCode, resultCode, data)
}

Java

private static final int SPEECH_REQUEST_CODE = 0;

// Create an intent that can start the Speech Recognizer activity
private void displaySpeechRecognizer() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// This starts the activity and populates the intent with the speech text.
    startActivityForResult(intent, SPEECH_REQUEST_CODE);
}

// This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent.
@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
        List<String> results = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        String spokenText = results.get(0);
        // Do something with spokenText.
    }
    super.onActivityResult(requestCode, resultCode, data);
}

Thao tác bằng giọng nói

Tính năng Thao tác bằng giọng nói và Hành động trong ứng dụng của Trợ lý hiện không được hỗ trợ, ngoại trừ các ứng dụng Wear OS ở Trung Quốc. Vui lòng đọc thêm về việc hỗ trợ tính năng Thao tác bằng giọng nói đối với thiết bị dành cho Trung Quốc.