2012年8月17日 星期五

voice_recognition: (2)



manifest.xml:


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

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/voice2"
        android:label="VoiceR2"
         >
        <activity
            android:label="@string/app_name"
            android:name=".Cq_VoiceRecognition02Activity"
             android:configChanges="orientation">
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

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

</manifest>


*************************************************************

main.java:


public class Cq_VoiceRecognition02Activity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */

ListView listview;
static final int check = 111;
protected static final boolean String = false;
private String [] stringData = {"fish","restaurant","car","menu"};

private TextView resultTextView;


OnClickListener transBtnListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voice_recognition);
     
        listview = (ListView)findViewById(R.id.listView1);
        Button b = (Button)findViewById(R.id.bVoice);
        b.setOnClickListener(this);
     
     
    }//end of onCreate

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Intent intent = new Intent (RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "不要害羞,請張口說話!");
startActivityForResult(intent, check);
}//end of onClick




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub

if (requestCode == check && resultCode == RESULT_OK) {
ArrayList<String> results = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
listview.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));

// 檢查一共幾筆資料
listview.setClickable(true);
Toast.makeText(
Cq_VoiceRecognition02Activity.this,
"一共查詢到 "
+ Integer
.toString(listview.getAdapter().getCount())
+ " 個項目", Toast.LENGTH_LONG).show();

// // 檢查與我們的資料庫是否有吻合的項目
// int inqueredItemCount = listview.getAdapter().getCount();
// int index = 0;
// for (int i = 0; i < inqueredItemCount; i++) {
//
// String item = (String) listview.getAdapter().getItem(i);
// Toast.makeText(this, item, Toast.LENGTH_SHORT).show();
//
// for (int j = 0; j < stringData.length; j++) {
//
// Toast.makeText(this, "stringData = " + stringData[j], Toast.LENGTH_SHORT).show();
// if (item == stringData[j]) {
// index++;
// Toast.makeText(this, "找到相符合的資料!", Toast.LENGTH_SHORT).show();
//
// }else {
// Toast.makeText(this, i + " 筆不是!", Toast.LENGTH_SHORT).show();
// }
// }
// }
//
//
// if (index == 0){
// Toast.makeText(this, "沒有資料!", Toast.LENGTH_SHORT).show();
// }else {
// Toast.makeText(this, "一共 " + index + " 筆", Toast.LENGTH_SHORT).show();
// }

// 設定點選的項目
listview.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {

// 取得Clicked String
String item = (String) listview.getAdapter().getItem(
position);

// 利用TOAST顯示點選的項目
Toast.makeText(Cq_VoiceRecognition02Activity.this,
Integer.toString(position), Toast.LENGTH_SHORT)
.show();
Toast.makeText(Cq_VoiceRecognition02Activity.this,
"您選了 \"" + item + "\"", Toast.LENGTH_SHORT).show();

resultTextView = (TextView)findViewById(R.id.resultTV);
resultTextView.setText("選擇的字句: " + item);


// test for ItemDemoPage
Intent intent = new Intent(Cq_VoiceRecognition02Activity.this, ItemDemoPage.class);
intent.putExtra("TEXT", item);
startActivity(intent);


}
});

}


super.onActivityResult(requestCode, resultCode, data);
}//end of OnActivityResult

 
}//end of Cq_VoiceRecognition02Activity



*************************************************
ItemDemoPage.java:


public class ItemDemoPage extends Activity{

public static final String ENCODING = "UTF-8";
String fileName = "speech_rec01.txt";
String message = "";

TextView tv;
Button editBtn;
Button doneBtn;
Button clearBtn;
Button playBtn;
EditText  et;

TextToSpeech tts;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.item_demo_play);

tv = (TextView) findViewById(R.id.clickedText);

editBtn = (Button)findViewById(R.id.editBtn);
doneBtn = (Button)findViewById(R.id.doneBtn);
clearBtn = (Button)findViewById(R.id.clearBtn);
playBtn = (Button) findViewById(R.id.playBtn);
et = (EditText)findViewById(R.id.editText1);

//tts = new TextToSpeech(getApplicationContext(), this);

 //自共同記憶區取得傳遞資料,變數名稱:TEXT
 Bundle extras = getIntent().getExtras();
 if (extras != null){
//   tv.setText(extras.getString("TEXT"));

//  String previousText = readFileData (fileName);
//  String secondPart = extras.getString("TEXT");
//  message = previousText + secondPart;

 String comma = ","; //句子間的分野
 message = extras.getString("TEXT") + comma;
 writeFileData(fileName, message);
 String result = readFileData (fileName);
 tv.setText(result);

 }

 //修改內容
 editBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
et.setText(tv.getText().toString());
}
});

 //完成
 doneBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
deleteFile(fileName);
String message = et.getText().toString();
writeFileData(fileName, message);

//完成時將keyboard 隱藏,以不至於檔到螢幕的View
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(et.getWindowToken(), 0);

String result = readFileData(fileName);
tv.setText(result);

et.setText("");

}
});


 //Clear all data
 clearBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
deleteFile(fileName);
tv.setText("(沒有資料,請回到前一頁!!!)");
et.setText("");
}
});


}// end of onCreate()


//寫入指定的資料
public void writeFileData (String fileName, String message) {
try {
FileOutputStream fos = openFileOutput(fileName, MODE_APPEND);
byte [] bytes = message.getBytes(); //將要寫入的字串轉換為byte陣列
fos.write(bytes); //將byte陣列寫入檔案
fos.close(); //關閉FileOutputStream物件
} catch (Exception e) {
e.printStackTrace();
}
} // end of writeFileData


public String readFileData (String fileName) {
String result = "";
try {
FileInputStream fis = openFileInput(fileName);
int length = fis.available();//獲取檔案長度
byte [] buffer = new byte[length];//新建byte陣列用於讀入資料
fis.read(buffer);//將檔案內容讀入到byte陣列中
result = EncodingUtils.getString(buffer, ENCODING);//將byte陣列轉換成指定格式的字串
fis.close();//關閉檔案輸入串流
} catch (Exception e) {
e.printStackTrace();
}
return result;//返回讀到的資料字串
}
}


***************************************************
不需要:
res/layout/main.xml:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

***************************************************
res/layout/item_demo_play.xml

<?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"
    android:background="@drawable/cloud01" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

    </EditText>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/editBtn"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:text="修改" />

        <Button
            android:id="@+id/doneBtn"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:text="完成" />
        
        <Button
            android:id="@+id/clearBtn"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:text="清除" />

    </LinearLayout>

    <TextView
        android:id="@+id/clickedText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:background="#987766"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:paddingLeft="5dp"
        android:textSize = "25dp"
        android:textColor = "#FFFFFF"
        android:gravity="center" />

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/playBtn"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:text="播放" />

    </LinearLayout>

</LinearLayout>

******************************************************
res/layout/voice_recognition.xml:

<?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" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="4dip"
        android:text="按下按鈕,開始說話!!!" />

    <Button
        android:id="@+id/bVoice"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="speakButtonClicked"
        android:text="按  我!" />
    <TextView
        android:id="@+id/resultTV"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="4dip"
        android:background="#987766"
        android:text="選擇的字句 :"
        android:textSize = "18dp" />
  
    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:layout_marginTop="5dp" >
    </ListView>
    

</LinearLayout>

沒有留言:

張貼留言