我正在创建一个使用文本到语音功能的Android应用程序我使用内置的文本到语音 我只想知道它是如何在android SDK中开发和维护的,如果有人知道一篇关于在android中开发文本到语音的文章我会被祝福
答案 0 :(得分: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>
答案 1 :(得分:0)
这thread会有所帮助。此外,API级别14中引入了TTS引擎实现的抽象类。请检查此link。您还可以阅读有关语音合成的information,以指导您如何实施。