IO --(5)讀出的檔案內容,套用TTS,將內容以機器合成聲音讀出
設計讓練習英聽的學生使用
Manifest.xml:
加上:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
***********************
main.java: 主程式
public class IO_TTSActivity extends Activity implements OnInitListener{
/** Called when the activity is first created. */
private final String SDCARD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
private final String FILE_PATH = "/fileio";
// private final String FILE_NAME = "test03.txt";
private final String ENCODING = "UTF-8";
private File mDataPath;
private FileInputStream fis = null;
TextView tv;
ListView lv;
String [] strings;
private TextToSpeech mTTS;
private Button listenBtn;
String result = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv= (TextView) findViewById(R.id.textView01);
tv.setMovementMethod(ScrollingMovementMethod.getInstance());
mDataPath = new File (SDCARD_PATH + FILE_PATH);
File [] filesArray = mDataPath.listFiles();
String fileNames = ""; // to receive every single file name
for (int i = 0; i < filesArray.length; i++) {
if (filesArray[i].isFile()) { //get rid of directories
String tempStr = ""; //會多出入境的字串
tempStr = filesArray[i].toString();
String strSub = ""; //to subtract the uneccessary part: /mnt/sdcard/fileio/ (19 個字元)
strSub = tempStr.substring(19, tempStr.length()).toString();
fileNames += strSub + "--";
}
}
//Handle to subtraction of the final "--" in fileNames string
String result = "";
result = fileNames.substring(0, fileNames.length()-2);
strings = TextUtils.split(result, "--");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(adapter);
lv.setOnItemClickListener(listener);
mTTS = new TextToSpeech(getApplicationContext(), this);//初始化
listenBtn = (Button) findViewById(R.id.button1);
listenBtn.setOnClickListener(btnListener);
}// end of onCreate
//選擇要唸出的檔案
private OnItemClickListener listener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
tv.setText(""); // clean tv in case of left-over
String aFileName = strings[position].toString();
readFile(aFileName);//Method: read the content of a file
//讀出所選檔案的內容;用QUEUE_ADD可以再接其他的字串;QUEUE_FLUSH將無法做到
mTTS.speak(result, TextToSpeech.QUEUE_ADD, null);
mTTS.playSilence(5000, TextToSpeech.QUEUE_ADD, null); //休息五秒
mTTS.speak(result, TextToSpeech.QUEUE_ADD, null); //又重複將內容朗誦一遍
}
};// end of listener
//看答案的按鈕
private OnClickListener btnListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText(result);
}
};
//讀出檔案的內容
private void readFile(String aFileName) {
try {
String mFileName = mDataPath + "/" + aFileName;
File fileContent = new File (mFileName);
fis = new FileInputStream(fileContent);
int length = fis.available();
byte [] buffer = new byte [length];
fis.read(buffer);
result = EncodingUtils.getString(buffer, ENCODING);
fis.close();
//tv.setText(result);
}catch (Exception e) {
e.printStackTrace();
}
} //end of readFile()
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
mTTS.speak("I'm ready", TextToSpeech.QUEUE_FLUSH, null);
}else {
System.out.println("Oops!");
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mTTS.shutdown();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (mTTS != null) {
mTTS.stop();
}
}
}
****************************************
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="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="#FF0000"
android:textSize="20dp"
android:text="選擇下列檔案練習英聽:" />
<TextView
android:id="@+id/textView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:maxLines="15"
android:scrollbarStyle="outsideInset"
android:scrollbars="vertical"
android:singleLine="false"
android:text=""
android:textColor="#00FF00"
android:textSize="20dp" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="看答案!" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_weight="0.85" >
</ListView>
</LinearLayout>
沒有留言:
張貼留言