2014年11月12日 星期三

Android Recognizer





範例程式碼

import........省略

public class MainActivity extends ActionBarActivity {

private static final String TAG = "SpeechRecognizerActivity";
    private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.tv = (TextView) this.findViewById(R.id.tv);
    }

    public void onText(View v) {
        Intent it = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        it.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        it.putExtra(RecognizerIntent.EXTRA_PROMPT, "請說...");
        this.startActivityForResult(it, 1);
    }

    public void onWeb(View v) {
        Intent it = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
        it.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
        it.putExtra(RecognizerIntent.EXTRA_PROMPT, "請說...");
        // 不用等結果
        this.startActivity(it);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent it) {
        if (requestCode != 1) {
            Log.d(TAG, "不是語音辨識");
            return;
        }
        if (resultCode != RESULT_OK) {
            Log.e(TAG, "語音辨識失敗");
            return;
        }
        List<String> list = it.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        StringBuilder text = new StringBuilder();
        for (String s : list) {
            text.append(s + "\n");
        }
        this.tv.setText(text);
    }

 
    //以下可省略
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
}

加入在:activity_main.xml


<Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onText"
        android:text="語音辨識" />

<Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onWeb"
        android:text="網頁搜尋" />

<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

<TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"/>


</ScrollView>



標記黃色區塊加入在:AndroidManifest.xml
可加可不加    要觸發onWeb(View v)才需要網路


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.recognizer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>








沒有留言:

張貼留言