歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 利用Android傳感器完成 Android語音輸入

利用Android傳感器完成 Android語音輸入

日期:2017/3/1 9:48:27   编辑:Linux編程

1、先曬曬效果圖:

2、manifest 文件訪問網絡配置:

<uses-permission Android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

3、layout配置文件代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/txtVoiceInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/sensor_voice_input_lb_title" >

<requestFocus />
</EditText>

<Button
android:id="@+id/btnStartVoiceInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sensor_voice_input_btn_startinput" />

</LinearLayout>

4、Java代碼:

private Button btnStartVoiceInput;

private int InputResultCode = 1;
private EditText txtVoiceInput;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sensor_voice_input);

//開始語音輸入按鈕綁定
txtVoiceInput = (EditText)findViewById(R.id.txtVoiceInput);
btnStartVoiceInput = (Button)findViewById(R.id.btnStartVoiceInput);
btnStartVoiceInput.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//調用系統傳感器語音輸入Api
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//必須輸入項語言模式
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

try {
startActivityForResult(intent, InputResultCode);
txtVoiceInput.setText("");
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"抱歉您的系統不支持語音識別輸入。",
Toast.LENGTH_LONG);
t.show();
}
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == InputResultCode && resultCode == RESULT_OK && data != null) {
//獲取解析的結果
ArrayList<String> voiceList = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtVoiceInput.setText(voiceList.get(0));
}
}

Android Speech to text Android API的核心是包 android.speech和類 android.speech.RecognizerIntent。我們觸發一個意圖(android.speech.RecognizerIntent)顯示對話框來識別語音輸入,這個Activity轉換語音為文本並把結果傳回我們正在調用的Activity。當我們調用android.speech.RecognizerIntent意圖時,必須使用 startActivityForResult()來接聽文本結果。

注意在上面的代碼中我們是怎樣創建並觸發意圖intent android.speech.RecognizerIntent的,同時使用.putExtra()方法添加了一個參數。調用RecognizerIntent時,必須提供RecognizerIntent.EXTRA_LANGUAGE_MODE,在這裡我們設置為 en-US。

由於我們的RecognizerIntent通過startActivityForResult()觸發,我們重寫了 onActivityResult(int requestCode, int resultCode, Intent data)方法來處理結果數據。RecognizerIntent會把語音轉換為文本並把結果通過鍵RecognizerIntent.EXTRA_RESULTS作為ArrayList傳回來。只有RESULT_OK返回時才會出現。我們只需要使用 txtText.setText()把從結果中拿到的文本設置到text view texText中。

在這裡值得注意的一件事是在不支持speech to text API的設備/Android版本中應該怎樣處理。在這種情況下,當我們視圖啟動Activity時ActivityNotFoundException異常會被拋出。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved