Android TTS不能说大量的文字 [英] Android TTS fails to speak large amount of text

查看:247
本文介绍了Android TTS不能说大量的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用大量的文字使用Android文字到语音。我使用默认的谷歌语音引擎。以下是我的代码。

I am trying to speak out large amount of text using android Text To Speech. I using default google speech engine. Below is my code.

 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无法读取。如果我删除大部分的文本,那么它会读取它。为什么会这样呢?

But the issue here is, when there is a large chunk of text (lets say you have extracted text from a web page) the TTS fails to read it. If I remove most of the text, then it will read it. Why is this happening?

当我要阅读大文本时,LogCat显示类似于

When I am about to read the large text, the LogCat however display something like this

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

请帮助。

推荐答案

不应超过预定义的长度,从 docs

The String length should not be longer than pre-defined length, from docs:


参数

要发音的文本字符串。不再是 getMaxSpeechInputLength()字符。

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

返回的值由 getMaxSpeechInputLength()可能因设备而异,但根据 AOSP源 4000

Returned value by getMaxSpeechInputLength() may vary from device to device, but according to AOSP source that is whopping 4000:

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

尽量不要超过这个限制:将输入的文本长度与该值进行比较如有需要,分开部分。

Try not to exceed that limit: compare input text length with that value and split into separate parts if necessary.

这篇关于Android TTS不能说大量的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆