所以我有一个原来的活动具有基本相同的确切code代表说,但我不得不把那个code到另一个活动。唯一的区别我可以告诉大家的是,文本到语音不叫在异步方法。口语发生在speakFull方法。 我得到这些错误:
在线失败:不绑定到TTS引擎
isSpeaking失败:不绑定到TTS引擎
我是新来的android的发展,我通过其他办法解决这一问题已经搜查了,我真的不能似乎找到一个解决方案,使我的工作。任何意见或帮助是AP preciated。
code:
包com.example.webview;
进口android.os.Bundle;
进口android.app.Activity;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.content.DialogInterface.OnClickListener;
进口android.content.Intent;
进口android.speech.tts.TextToSpeech;
进口android.text.method.ScrollingMovementMethod;
进口android.view.Menu;
进口android.view.View;
进口android.widget.Button;
进口android.widget.TextView;
公共类读取扩展活动实现TextToSpeech.OnInitListener,OnClickListener {
布尔暂停= FALSE;
字符串leftToRead = NULL;
字符串资源= NULL;
@燮pressWarnings(德precation)
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.read_out);
意向意图= getIntent();
RES = intent.getExtras()的getString(回应)。
TextView的textv =(TextView中)findViewById(R.id.textView1);
textv.setText(RES);
textv.setMovementMethod(新ScrollingMovementMethod());
android.view.Display显示=((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE))getDefaultDisplay()。
textv.setHeight((int)的(display.getHeight()* 0.76));
leftToRead = speakFull(RES);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
返回true;
}
公共字符串speakFull(字符串文本){
的System.out.println(说到:+文字);
TextToSpeech TTS =新TextToSpeech(这一点,这一点);
的System.out.println(说起);
的String []句子= text.split(\ N | \\(\\ D)|(小于!?!。\\ D)\\); //正则表达式,简单地将文本的身体进入该机构被存储在一个字符串数组的句子。
的for(int i = 0; I< sentences.length;我++){
如果(tts.isSpeaking()及!&安培;!暂停){
的System.out.println(说到:+ I);
tts.speak(句子[我],TextToSpeech.QUEUE_FLUSH,NULL);
}否则,如果(暂停){
的System.out.println(暂停);
字符串暂停=;
对于(INT J = - 1; J< sentences.length; J ++){
暂停+ =句子[J]。
}
返回暂停;
}其他{
一世 - ;
}
如果(ⅰ== sentences.length - 1){
返回消息001:完成;
}
}
返回null;
}
@覆盖
公共无效的OnInit(INT为arg0){
// TODO自动生成方法存根
}
公共无效clickPause(视图v){
如果(暂停){
暂停= FALSE;
按钮B =(按钮)findViewById(R.id.button1);
b.setText(播放);
}其他{
暂停= TRUE;
按钮B =(按钮)findViewById(R.id.button1);
b.setText(暂停);
如果(leftToRead == NULL){
leftToRead = speakFull(RES);
}其他{
leftToRead = speakFull(leftToRead);
}
}
}
@覆盖
公共无效的onClick(DialogInterface为arg0,INT ARG1){
// TODO自动生成方法存根
}
}
解决方案
您只能叫的OnInit后发言()
()被调用。因此,移动你的TTS中的onCreate说话code到的OnInit()
@覆盖
公共无效的OnInit(INT状态){
如果(状态== TextToSpeech.SUCCESS){
leftToRead = speakFull(RES);
}
和初始化暂停为true 布尔暂停= TRUE;
So I had an original activity with basically the same exact code for speaking, but I had to move that code into another activity. The only difference I can tell is that the text to speech is not called in an asynchronous method. The speaking occurs in the speakFull method. I get these errors:
speak failed: not bound to TTS engine
isSpeaking failed: not bound to TTS engine
I'm new to android development, I've searched through other solutions to this problem, and I can't really seem to find a solution to make mine work. Any advice, or help is appreciated.
Code:
package com.example.webview;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ReadOut extends Activity implements TextToSpeech.OnInitListener, OnClickListener {
boolean paused = false;
String leftToRead = null;
String res = null;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.read_out);
Intent intent = getIntent();
res = intent.getExtras().getString("response");
TextView textv = (TextView) findViewById(R.id.textView1);
textv.setText(res);
textv.setMovementMethod(new ScrollingMovementMethod());
android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
textv.setHeight((int)(display.getHeight()*0.76));
leftToRead = speakFull(res);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
public String speakFull(String text){
System.out.println("Speaking: " + text);
TextToSpeech tts = new TextToSpeech(this, this);
System.out.println("Speaking");
String[] sentences = text.split("\n|\\.(?!\\d)|(?<!\\d)\\."); // Regex that splits the body of text into the sentences of that body which are stored in a String array.
for(int i = 0; i < sentences.length; i++){
if(!tts.isSpeaking() && !paused){
System.out.println("Speaking: " + i);
tts.speak(sentences[i], TextToSpeech.QUEUE_FLUSH, null);
}else if(paused){
System.out.println("Paused");
String paused = "";
for(int j = i - 1; j < sentences.length; j++){
paused += sentences[j];
}
return paused;
}else{
i--;
}
if(i == sentences.length - 1){
return "Message 001: Complete";
}
}
return null;
}
@Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
}
public void clickPause(View v){
if(paused){
paused = false;
Button b = (Button) findViewById(R.id.button1);
b.setText("Play");
}else{
paused = true;
Button b = (Button) findViewById(R.id.button1);
b.setText("Pause");
if(leftToRead == null){
leftToRead = speakFull(res);
}else{
leftToRead = speakFull(leftToRead);
}
}
}
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
}
解决方案
You can only call speak()
after onInit() was called. So move your tts speak code in onCreate to onInit()
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
leftToRead = speakFull(res);
}
and initialize pause to true boolean paused = true;