วิธีรวม Android Speech To Text (เสียงเป็นข้อความ)
Android รองรับ API Text-to-Speech ในตัวของ Google โดยใช้ RecognizerIntent.ACTION_RECOGNIZE_SPEECH ในตัวอย่างนี้สาธิตวิธีรวม Android Speech To Text
ขั้นตอนที่ 1 - สร้างโปรเจกต์ใหม่ใน Android Studio
ไปที่ File → New Project และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโปรเจกต์ใหม่
ขั้นตอนที่ 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>
ในโค้ดด้านบน เราสร้าง TextView หนึ่งตัวและ ImageView หนึ่งตัว เมื่อผู้ใช้คลิกที่ ImageView จะเรียก Google Speech to Text API และเพิ่มข้อความใน TextView
ขั้นตอนที่ 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 && data != null) {
ArrayList<String> 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 && data != null) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
textView.setText(result.get(0));
}
break;
}
}
}
ในโค้ดด้านบน เราจะได้ผลลัพธ์เป็น ArrayList ดังนั้นเราจึงนำตำแหน่งศูนย์จาก ArrayList มาเพิ่มใน TextView
ลองรันแอปพลิเคชันของคุณ สมมติว่าคุณเชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์แล้ว เพื่อรันแอปจาก Android Studio ให้เปิดไฟล์กิจกรรมของโปรเจกต์หนึ่งและคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก และตรวจสอบอุปกรณ์ซึ่งจะแสดงหน้าจอเริ่มต้น

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

ตอนนี้เราให้อินพุตว่า "Hey GOOGLE" จะนำผลลัพธ์มาแสดงดังภาพด้านล่าง
