음성 입력

모든 Wear OS 기기에는 마이크가 제공되므로 사용자가 음성으로 기기와 상호작용할 수 있습니다. 상호작용 유형은 세 가지로 나눌 수 있습니다.

  • 오디오 녹음
  • 자유 형식 음성 입력 얻기
  • 음성 액션

오디오 녹음

Wear OS 기기에서 오디오 녹음은 휴대전화에서와 동일한 방식으로 작동합니다. Android에서 오디오를 녹음하는 방법에 관한 자세한 내용은 MediaRecorder 문서를 참고하세요. GitHub의 Wear 스피커 샘플에서 샘플 구현을 확인해도 됩니다.

자유 형식 음성 입력 얻기

시스템의 내장 음성 인식기 활동을 호출하여 사용자로부터 음성 입력을 얻습니다. 음성 입력을 사용하여 메시지를 보내거나 검색을 실행합니다.

앱에서 ACTION_RECOGNIZE_SPEECH 작업을 사용하여 startActivityForResult()를 호출합니다. 그러면 음성 인식 활동이 시작되기 때문에 onActivityResult()에서 결과를 처리할 수 있습니다.

다음 코드 샘플은 음성 인식 활동을 시작하고 처리하는 방법을 보여줍니다.

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);
}

음성 액션

중국의 Wear OS 앱을 제외하고 현재 음성 액션과 어시스턴트 앱 작업은 지원되지 않습니다. 중국의 음성 액션 지원에 관해 자세히 알아보세요.