添加语音功能

语音操作是穿戴式设备体验的重要组成部分。借助此功能,用户无需动手就能快速执行操作。Wear OS by Google 谷歌提供了两种语音操作:

系统提供的语音操作
这种语音操作基于任务并内置于 Wear 平台。说出语音操作时,在您要启动的 Activity 中过滤它们。示例包括“添加记事”或“设置闹钟”。
应用提供的语音操作
这种语音操作基于应用,您可以像声明启动图标一样声明它们。用户可以说出“启动(您的应用名称)”以使用这些语音操作,此时您指定的 Activity 将会启动。

声明系统提供的语音操作

Wear OS 平台提供了一些基于用户操作(如“添加记事”或“设置闹钟”)的语音 intent。这可让用户说出他们想要执行的操作,然后让系统确定要启动的最佳 Activity。

当用户说出语音操作时,您的应用可以过滤为了启动 Activity 而触发的 intent。如果您要启动一项服务以在后台执行某项操作,可显示一个 Activity 作为视觉提示,并在此 Activity 中启动该服务。如果您想去除视觉提示,请务必调用 finish()

例如,对于“添加记事”命令,可声明以下 intent 过滤器以启动一个名为 MyNoteActivity 的 Activity:

      <activity android:name="MyNoteActivity">
          <intent-filter>
              <action android:name="android.intent.action.SEND" />
              <category android:name="com.google.android.voicesearch.SELF_NOTE" />
          </intent-filter>
      </activity>
    

下面列出了 Wear 平台支持的语音 Intent:

名称 示例语句 Intent
叫辆车/出租车 “Ok Google,叫辆出租车”

“Ok Google,叫辆车”
操作
com.google.android.gms.actions.RESERVE_TAXI_RESERVATION
添加记事 “Ok Google,添加记事”

“Ok Google,自我提醒”
操作
android.intent.action.SEND
类别
com.google.android.voicesearch.SELF_NOTE
额外项
android.content.Intent.EXTRA_TEXT - 带有记事正文的字符串
设置闹钟 “Ok Google,设置早上 8 点的闹钟”

“Ok Google,明天 6 点叫醒我”
操作
android.intent.action.SET_ALARM
额外项
android.provider.AlarmClock.EXTRA_HOUR - 一个整数,表示闹铃的小时。

android.provider.AlarmClock.EXTRA_MINUTES - 一个整数,表示闹铃的分钟

(这两个额外项是可选的,要么都不提供,要么都提供)

设置定时器 “Ok Google,设置一个 10 分钟的定时器”
操作
android.intent.action.SET_TIMER
额外项
android.provider.AlarmClock.EXTRA_LENGTH - 一个整数,范围为 1 到 86400(24 小时的秒数),表示定时器的时长
启动秒表 “Ok Google,启动秒表”
操作
com.google.android.wearable.action.STOPWATCH
开始/停止骑自行车 “Ok Google,开始骑车”

“Ok Google,开始骑自行车”

“Ok Google,停止骑车”
操作
vnd.google.fitness.TRACK
MimeType
vnd.google.fitness.activity/biking
额外项
actionStatus - 一个字符串,在开始时值为 ActiveActionStatus,在停止时值为 CompletedActionStatus
开始/停止跑步 “Ok Google,跟踪我的跑步”

“Ok Google,开始跑步”

“Ok Google,停止跑步”
操作
vnd.google.fitness.TRACK
MimeType
vnd.google.fitness.activity/running
额外项
actionStatus - 一个字符串,在开始时值为 ActiveActionStatus,在停止时值为 CompletedActionStatus
开始/停止锻炼 “Ok Google,开始锻炼”

“Ok Google,跟踪锻炼”

“Ok Google,停止锻炼”
操作
vnd.google.fitness.TRACK
MimeType
vnd.google.fitness.activity/other
额外项
actionStatus - 一个字符串,在开始时值为 ActiveActionStatus,在停止时值为 CompletedActionStatus
显示心率 “Ok Google,我的心率是多少?”

“Ok Google,我的 bpm 是多少?”
操作
vnd.google.fitness.VIEW
MimeType
vnd.google.fitness.data_type/com.google.heart_rate.bpm
显示步数 “Ok Google,我走了多少步?”

“Ok Google,我的步数是多少?”
操作
vnd.google.fitness.VIEW
MimeType
vnd.google.fitness.data_type/com.google.step_count.cumulative

如需查看有关注册平台 Intent 和访问其中包含的额外项信息的文档,请参阅常见 Intent

声明应用提供的语音操作

如果没有适合您的平台语音 intent,您可以通过“启动 MyActivityName”语音操作直接启动您的应用。

在手持设备上,注册“启动”操作与注册启动器图标相同。但是,应用不会请求启动器中的应用图标,而会请求语音操作。

要指定在“启动”后要说的内容,可为您要启动的 Activtiy 指定 label 属性。例如,以下 Intent 过滤器可识别“启动 MyRunningApp”语音操作并启动 StartRunActivity

    <application>
      <activity android:name="StartRunActivity" android:label="MyRunningApp">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
      </activity>
    </application>
    

获取自由格式的语音输入

除使用语音操作启动 Activity 外,您还可以调用系统内置的语音识别程序 Activity 来获取用户的语音输入。这对于获取用户输入而后对其进行处理(如执行搜索或将其作为消息发送)非常有用。

在您的应用中,使用 ACTION_RECOGNIZE_SPEECH 操作调用 startActivityForResult()。这将启动语音识别 Activity,然后您可以在 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)
        }
        // Start the activity, the intent will be populated 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);
    // Start the activity, the intent will be populated 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);
    }
    

另请参阅以下相关资源: