我正在尝试为Android实现TTS应用程序。这是我到目前为止编写的代码:
import android.app.Activity;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Locale;
public class AlarmActivity extends Activity implements OnClickListener, TextToSpeech.OnInitListener {
private TextToSpeech mTts;
private static final String TAG = "TextToSpeechDemo";
private static final int MY_DATA_CHECK_CODE = 1234;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.alarm);
Button btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
btnAdd.setEnabled(false);
TextView txt = (TextView) findViewById(R.id.txt);
txt.setText("OnCreate");
// Fire off an intent to check if a TTS engine is installed
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
TextView txt = (TextView) findViewById(R.id.txt);
if (requestCode == MY_DATA_CHECK_CODE)
{
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
{
// success, create the TTS instance
txt.setText("Done result");
mTts = new TextToSpeech(this, this);
mTts.setLanguage(Locale.US);
Button btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setEnabled(true);
}
else
{
txt.setText("Missing");
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(
TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
@Override
public void onDestroy()
{
// Don't forget to shutdown!
if (mTts != null)
{
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
@Override
public void onClick(View v) {
TextView txt = (TextView) findViewById(R.id.txt);
txt.setText("Click");
String myText1 = "Did you sleep well?";
String myText2 = "I hope so, because it's time to wake up.";
mTts.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);
mTts.speak(myText2, TextToSpeech.QUEUE_ADD, null);
}
@Override
public void onInit(int status) {
TextView txt = (TextView) findViewById(R.id.txt);
txt.setText("status 0");
if (status == TextToSpeech.SUCCESS) {
txt.setText("status 1");
int result = mTts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
txt.setText("status 2");
} else {
Button btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setEnabled(true);
txt.setText("status 3");
}
} else {
txt.setText("status 4");
Log.e(TAG, "Could not initialize TextToSpeech.");
}
}
}
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alarms"
android:id="@+id/txt" />
<Button android:id="@+id/btnAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Say" />
</LinearLayout>
在netbeans ADB日志中,我可以看到以下内容:
13:08:41.314 163 INFO ActivityManager Starting activity: Intent { act=android.speech.tts.engine.CHECK_TTS_DATA cmp=com.svox.pico/.CheckVoiceData }
13:08:41.504 163 INFO ActivityManager Displayed activity org.me.talkingdroid/.MainActivity: 2790 ms (total 2790 ms)
13:08:41.544 163 WARN ActivityManager Binding with unknown activity: android.os.BinderProxy@46d91ac8
13:08:41.634 264 DEBUG [ScrollKPI] drawScreenCache takes 222ms, drawThumbnailCache takes 106ms
13:08:43.214 163 VERBOSE KeyInputQueue Enqueueing: MotionEvent{470684e0 action=0 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}
13:08:43.214 163 INFO WindowManager dispatchPointer MotionEvent{470684e0 action=0 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}
13:08:43.214 163 INFO WindowManager Delivering pointer QueuedEvent{470bdcb8 MotionEvent{470684e0 action=0 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}} to Window{46e0c640 org.me.talkingdroid/org.me.talkingdroid.MainActivity paused=false}
13:08:43.264 163 VERBOSE KeyInputQueue Enqueueing: MotionEvent{470684e0 action=1 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}
13:08:43.264 163 INFO WindowManager dispatchPointer MotionEvent{470684e0 action=1 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}
13:08:43.264 163 INFO WindowManager Delivering pointer QueuedEvent{46f34300 MotionEvent{470684e0 action=1 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}} to Window{46e0c640 org.me.talkingdroid/org.me.talkingdroid.MainActivity paused=false}
13:08:43.284 10637 INFO TTS received: Did you sleep well?
13:08:43.284 10637 INFO TTS received: I hope so, because it's time to wake up.
13:08:46.714 10038 DEBUG dalvikvm GC_EXPLICIT freed 417 objects / 26192 bytes in 82ms
13:08:47.994 10637 DEBUG dalvikvm Debugger has detached; object registry had 398 entries
很明显,我试图输出的文本是从日志发送的,但是我什么也听不到来自我手机的信息。可能是什么问题?
答案 0 :(得分:4)
这似乎是因为手机在USB调试模式下连接到计算机。一旦禁用,TTS完美运行!
答案 1 :(得分:1)
你的代码更像是android doc。我刚写了这个演示代码。我相信如果您在下一行中将requestCode
替换为resultCode
,它将正常工作。
if (requestCode == MY_DATA_CHECK_CODE)
这是我的代码:
public class TTS_demoActivity extends Activity implements OnClickListener,OnInitListener {
private EditText mContent;
private Button mSpeak;
private String mTemp;
private static final int REQ_TTS_STATUS_CHECK = 1;
private static final String TAG = "TTS Demo";
private TextToSpeech mTts;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContent = (EditText) findViewById(R.id.etcontent);
mSpeak = (Button) findViewById(R.id.btnspeak);
mSpeak.setOnClickListener(this);
init();
}
/*
* 初始化方法检测TTS所需要的数据是否存在
*/
private void init() {
Intent mCheckIntent=new Intent();
mCheckIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(mCheckIntent, REQ_TTS_STATUS_CHECK);
}
/*
* 对检测结果进行分类处理
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==REQ_TTS_STATUS_CHECK)
{
switch (resultCode) {
case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS:
mTts = new TextToSpeech(this, this);
Log.v(TAG, "tts engine is instance");
break;
case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA:
//文件已经损坏
case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME:
//缺少发音文件
case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA:
//数据文件丢失
//从新更新TTS数据文件
Intent mUpdateData=new Intent();
mUpdateData.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(mUpdateData);
break;
case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL:
//检测失败应该重新检测
Log.v(TAG, "TTS engine checked fail");
break;
default:
Log.v(TAG, "Got a failure. TTS apparently not available");
break;
}
}
}
@Override
public void onInit(int status) {
if(status==TextToSpeech.SUCCESS){
int result=mTts.setLanguage(Locale.US);
if(result==TextToSpeech.LANG_NOT_SUPPORTED||result==TextToSpeech.LANG_MISSING_DATA)
{
Log.v(TAG, "language is not available");
}else{
mTts.speak("start complete", TextToSpeech.QUEUE_ADD, null);
mSpeak.setEnabled(true);
}
}
}
@Override
public void onClick(View arg0) {
mTemp= mContent.getText().toString();
if(mTemp!=""||mTemp.trim()!=""){
mTts.speak(mTemp, TextToSpeech.QUEUE_ADD,null);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mTts!=null){
mTts.shutdown();
}
}
@Override
protected void onStop() {
super.onStop();
if(mTts!=null){
mTts.stop();
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
}
答案 2 :(得分:1)
此线程指的是SpeechSynthesis数据安装程序的问题。我的问题也是来自TTS程序的'没有声音'(类似于所示),否则会正确执行。在我的情况下,这是由于我的平板电脑上没有安装SpeechSynthesis数据。 在我的平板电脑上安装后,我的程序甚至可以在调试模式下工作,并且连接了电缆。
对于TTS的新用户,像我一样,我在下面列出了我在Android 4.0.4上安装speechsynthesis数据所做的工作。它们在其他版本上可能有所不同,但应该是相似的。
开始'设置' - 选择:' 设置&gt;语言和输入&gt;文字转语音输出' 然后在“首选引擎下,选择” Google文字转语音引擎“并点击该选项的RHS上的'设置'符号。 这显示了三个选项:首先选择(1)'语言'然后选择(2)'Google TTS的设置'并选中'自动更新语音'< / em>和'仅使用Wi-Fi'*然后最终选择(3)'安装语音数据'; (必须启用Wi-Fi才能下载数据)
感谢stackoverflow,问题和响应给出了我能够解决我的问题,这不是代码,而是我平板电脑上TTS的配置。我还了解到代码并不总是问题。
答案 3 :(得分:0)
我必须将我的默认语言更改为手机上的文本到语音支持的语言才能使其正常工作:
设置 - &gt;语言和输入 - &gt;语言。
支持:英语(英国)&amp;英语(美国)。
不支持:英语(IE)。
答案 4 :(得分:0)
我在运行android的Raspberry pi 3上遇到类似的问题,即TTS仅通过USB端口播放,直到我在带有标志TextToSpeech.setAudioAttributes(AudioAttributes)的TextToSpeech对象上运行AudioAttributes.FLAG_AUDIVILITY_ENFORCED为止,如您所见。以下代码:
public class CustomTTS extends TextToSpeech {
private static final String UTTERANCE_ID =
"com.example.androidthings.bluetooth.audio.UTTERANCE_ID";
public CustomTTS(Context context, TextToSpeech.OnInitListener listener){
super(context, listener);
setOnUtteranceProgressListener(new MyProgListener());
try {
AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder().
setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION_SIGNALLING).
setContentType(AudioAttributes.CONTENT_TYPE_SPEECH).
setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED);
setAudioAttributes(audioAttributes.build());
} catch (Exception e) {
e.printStackTrace();
}
}
}