Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

จะรวม Android Speech To Text ได้อย่างไร


Android รองรับ Google inbuilt text เพื่อพูด API โดยใช้ RecognizerIntent.ACTION_RECOGNIZE_SPEECH ในตัวอย่างนี้สาธิตวิธีผสานรวมเสียงพูดของ Android เข้ากับข้อความ

ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่

ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน res/layout/activity_main.xml

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:app = "https://schemas.android.com/apk/res-auto"
   xmlns:tools = "https://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;
      }
   }
}

ในโค้ดด้านบนนี้ เราจะได้ผลลัพธ์เป็น Array List ดังนั้นเราจึงไม่มีตำแหน่งเป็นศูนย์จากรายการอาร์เรย์และผนวกเข้ากับมุมมองข้อความ

มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก แล้วตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ -

จะรวม Android Speech To Text ได้อย่างไร

เมื่อผู้ใช้คลิกปุ่มไมค์จะเรียก Google API ดังแสดงด้านล่าง -

จะรวม Android Speech To Text ได้อย่างไร

ตอนนี้เราได้ป้อนข้อมูลเป็น "เฮ้ GOOGLE" มันจะผนวกผลลัพธ์ตามที่แสดงด้านล่าง -

จะรวม Android Speech To Text ได้อย่างไร