我正在使用文本到语音功能创建一个 android 应用程序我使用了内置的文本到语音我只想知道它是如何在 android SDK 中开发和维护的,如果有人知道关于在 android 中开发文本到语音的术语论文我会被祝福的
问问题
1324 次
2 回答
0
幸运的是我也在这工作,拿我的代码
package com.example.texttospeech;
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener, OnInitListener {
private TextToSpeech tts;
EditText editxt;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this , this);
editxt = (EditText) findViewById(R.id.editText1);
b1 = (Button) findViewById(R.id.read);
b1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.read:
convert_text();
break;
default:
break;
}
}
private void convert_text() {
String speech = editxt.getText().toString();
tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
@Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS){
int result = tts.setLanguage(Locale.getDefault());
if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
Log.e("DEBUG" , "Language Not Supported");}
else{
b1.setEnabled(true);
convert_text();
}
}
else{
Log.i("DEBUG" , "MISSION FAILED");
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (tts != null){
tts.stop();
tts.shutdown();
}
}
}
mylayout activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="21sp"
android:layout_marginTop="23dp"
android:text="Text To Speech Test" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:ems="10" />
<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/editText1"
android:layout_marginLeft="46dp"
android:layout_marginTop="50dp"
android:text="Read" />
</RelativeLayout>
于 2014-03-31T13:59:36.060 回答