gpt4 book ai didi

java - Android TTS 无法说出大量文字

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:42 24 4
gpt4 key购买 nike

我正在尝试使用 Android Text To Speech 说出大量文本。我使用默认的谷歌语音引擎。下面是我的代码。

 public class Talk extends Activity implements TextToSpeech.OnInitListener {

private ImageView playBtn;

private EditText textField;

private TextToSpeech tts;
private boolean isSpeaking = false;

private String finalText;

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

//Intialize the instance variables
playBtn = (ImageView)findViewById(R.id.playBtn);

textField = (EditText)findViewById(R.id.textField);


//Resister the listeners
playBtn.setOnClickListener(new PlayBtnAction());

//Other things
tts = new TextToSpeech(this,this);

//Get the web page text if called from Share-Via
if (Intent.ACTION_SEND.equals(getIntent().getAction()))
{

new GetWebText().execute("");

}
}


//This class will execute the text from web pages
private class GetWebText extends AsyncTask<String,Void,String>
{

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
String websiteText = "";


try {
//Create a URL for the desired page
URL url = new URL(text);
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
StringBuffer strBuffer = new StringBuffer("");

while ((str = in.readLine()) != null)
{
strBuffer.append(str+"\n"+"\n");
}


in.close();

String html = strBuffer.toString();

Document doc = Jsoup.parse(html);
websiteText = doc.body().text(); // "An example link"
//Toast.makeText(this, websiteText, Toast.LENGTH_LONG).show();

}
catch(Exception e)
{
Log.e("web_error", "Error in getting web text",e);
}
return websiteText;
}

@Override
protected void onPostExecute(String result)
{
textField.setText(result);
}

}

}

//Class to speak the text
private class PlayBtnAction implements OnClickListener
{


@Override
public void onClick(View v)
{

// TODO Auto-generated method stub
if(!isSpeaking)
{
isSpeaking = true;
//speak(textField.getText().toString());
finalText = textField.getText().toString();
new SpeakTheText().execute(finalText);
isSpeaking = false;
}
else
{
isSpeaking = false;
tts.stop();
}



}

}



@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status==TextToSpeech.SUCCESS)
{
int result = tts.setLanguage(Locale.UK);

if(result==TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
{
Toast.makeText(this, "Language Not Supported", Toast.LENGTH_LONG).show();
}
}

}


//This class will speak the text
private class SpeakTheText extends AsyncTask<String,Void,String>
{

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
tts.speak(params[0], TextToSpeech.QUEUE_FLUSH, null);
return null;
}

}


@Override
public void onDestroy()
{
if(tts!=null)
{
tts.stop();
tts.shutdown();

}
super.onDestroy();
}

}

但这里的问题是,当有大量文本时(假设您从网页中提取了文本),TTS 无法读取它。如果我删除了大部分文本,它就会读取它。为什么会这样?

当我要阅读大文本时,LogCat 却显示这样的内容

10-11 07:26:05.566: D/dalvikvm(2638): GC_CONCURRENT freed 362K, 44% free 3597K/6312K, paused 17ms+8ms, total 93ms

最佳答案

字符串长度不应超过预定义的长度,来自 docs :

Parameters

text The string of text to be spoken. No longer than getMaxSpeechInputLength() characters.

getMaxSpeechInputLength() 的返回值可能因设备而异,但根据 AOSP source那是惊人的 4000:

/**
* Limit of length of input string passed to speak and synthesizeToFile.
*
* @see #speak
* @see #synthesizeToFile
*/
public static int getMaxSpeechInputLength() {
return 4000;
}

尽量不要超过该限制:将输入文本长度与该值进行比较,并在必要时拆分为单独的部分。

关于java - Android TTS 无法说出大量文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19312536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com