語音與錄音系統應用

1. activity_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" >

    <EditText
        android:id="@+id/info_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/select_tts_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select TTS engine" />

    <Button
        android:id="@+id/speak_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Speak" />

    <Button
        android:id="@+id/record_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Record" />

    <Button
        android:id="@+id/recplay_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:text="Record Play" />


</LinearLayout>

2. MainActivity2.java

import java.io.File;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Environment;

public class MainActivity2 extends Activity implements OnInitListener {

private TextToSpeech myTTS;
private Button select_tts_btn, speak_btn, record_btn, recplay_btn;
private EditText info;
private MediaPlayer player = null;
private File file;
private final int setTTs = 1;
private String fileName = Environment.getExternalStorageDirectory()+"/record.ogg";

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

select_tts_btn = (Button) this.findViewById(R.id.select_tts_btn);
speak_btn = (Button) this.findViewById(R.id.speak_btn);
info = (EditText) this.findViewById(R.id.info_btn);
record_btn = (Button) this.findViewById(R.id.record_btn);
recplay_btn = (Button) this.findViewById(R.id.recplay_btn);
myTTS = new TextToSpeech(this, this);

select_tts_btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
try {

Intent intent = new Intent(
"com.android.settings.TTS_SETTINGS");
MainActivity2.this.startActivityForResult(intent, setTTs);
} catch (Exception io) {
Toast.makeText(MainActivity2.this, "Error",
Toast.LENGTH_SHORT).show();
}
}

});

speak_btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
try {
myTTS.speak(info.getText().toString(),
TextToSpeech.QUEUE_ADD, null);
} catch (Exception io) {
Toast.makeText(MainActivity2.this, "Please input text",
Toast.LENGTH_SHORT).show();
}
}

});

record_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 把TTS 語音合成的結果保存為音頻檔

file = new File(fileName);
if (file.exists()) {
file.delete();
}
// 把語音合成的結果保存到檔中
if (TextToSpeech.SUCCESS == myTTS.synthesizeToFile(info
.getText().toString(), null, fileName)) {
Toast.makeText(getBaseContext(), "sound file created!",
Toast.LENGTH_SHORT).show();
recplay_btn.setEnabled(true);
} else {
Toast.makeText(getBaseContext(),
"failed to create sound file!", Toast.LENGTH_SHORT)
.show();
}
}
});
recplay_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 播放保存著的音頻檔
try {
player = new MediaPlayer();
player.setDataSource(fileName);
player.prepare();
player.start();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getBaseContext(),
"failed to play sound file!", Toast.LENGTH_SHORT)
.show();
e.printStackTrace();
}
}
});
}

@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
myTTS.setEngineByPackageName(myTTS.getDefaultEngine());
Toast.makeText(
MainActivity2.this,
"TTS Engine : " + myTTS.getDefaultEngine().toString()
+ "\n" + "Language : "
+ myTTS.getLanguage().toString(),
Toast.LENGTH_SHORT).show();

} else {
Toast.makeText(MainActivity2.this, "Error Engine",
Toast.LENGTH_SHORT).show();
}

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == setTTs) {
myTTS = new TextToSpeech(this, this);
}
}

protected void onDestroy() {
if (myTTS != null) {
myTTS.stop();
myTTS.shutdown();
}
super.onDestroy();
}

}

沒有留言: