如何整合 Android 語音到文字?
Android 使用 RecognizerIntent.ACTION_RECOGNIZE_SPEECH 支援 Google 內建的文字轉語音 API。本示例演示瞭如何整合 Android 語音到文字。
步驟 1 - 在 Android Studio 中建立一個新專案,轉到檔案 ⇒ 新建專案,並填寫所有必需的詳細資訊以建立新專案。
步驟 2 - 將以下程式碼新增到 res/layout/activity_main.xml。
<?xml version = "1.0" encoding = "utf-8"?> <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity"> <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_alignParentBottom = "true" android:layout_centerInParent = "true" android:orientation = "vertical" android:layout_height = "wrap_content"> <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>
在上述程式碼中,我們建立了一個文字檢視和一個影像檢視。當用戶點選影像檢視時,它將呼叫 Google 語音到文字 API 並將文字新增到文字檢視中。
步驟 3 - 將以下程式碼新增到 src/MainActivity.java
package com.example.andy.myapplication; import android.content.ActivityNotFoundException; import android.content.Intent; import android.speech.RecognizerIntent; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.Locale; public class MainActivity extends AppCompatActivity { private 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); ImageView speak = findViewById(R.id.speak); speak.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { 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 a) { Toast.makeText(getApplicationContext(), "Sorry your device not supported", Toast.LENGTH_SHORT).show(); } } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQ_CODE: { if (resultCode = = RESULT_OK && null ! = data) { ArrayList result = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); textView.setText(result.get(0)); } break; } } } }
在上面的程式碼中,當用戶點選 imageview 時,它將呼叫如下所示的 intent -
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 a) { Toast.makeText(getApplicationContext(), "Sorry your device not supported", Toast.LENGTH_SHORT).show(); }
在上面的程式碼中,我們呼叫了 Google API,並將結果獲取到 onActivityResult() 中,如下所示 -
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQ_CODE: { if (resultCode = = RESULT_OK && null ! = data) { ArrayList result = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); textView.setText(result.get(0)); } break; } } }
在上面的程式碼中,我們將結果作為陣列列表獲取,因此我們從陣列列表中獲取第零個位置並將其追加到文字檢視中。
讓我們嘗試執行您的應用程式。我假設您已將您的實際 Android 移動裝置連線到您的計算機。要從 android studio 執行應用程式,請開啟您專案的其中一個活動檔案,然後單擊工具欄中的執行 圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -
當用戶點選麥克風按鈕時,它將呼叫 Google API,如下所示 -
現在我們輸入了“Hey GOOGLE”。它將追加結果,如下所示 -
點選 此處 下載專案程式碼
廣告