Android TTS fails to speak large amount of text

后端 未结 5 1386
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 05:01

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 extend         


        
相关标签:
5条回答
  • 2021-01-02 05:08

    It is worse than the 4000 characters limit in practice on Android. There are some TTS engines that limit the input length a lot more. For example Nuance.tts and vocalizer.tts engines won't speak any string longer than about 512 characters (from my tests some time ago). Today I hit a limit of below 300 characters in es.codefactory.eloquencetts package, which simply crashes if the string I send to it is more than 256-300 characters. I divide the contents into sentences, and guard for sentences longer than the above limit, further sub-dividing them in my app code.

    Greg

    0 讨论(0)
  • 2021-01-02 05:24

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

    Parameters

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

    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.

    0 讨论(0)
  • 2021-01-02 05:25

    If you follow ozbek's advice you should be fine. I too have large text files that I want spoken. I simply used the streamreader method and everything works fine. heres' PART of my code. it's the part that you should use. My code does a bit more than you want but it works for me and may work for you.

    Dim sReader As StreamReader = New StreamReader(Story_file) Try

            Do Until EndOfStream '= True
                Dim line_to_speak As String = sReader.ReadLine
                Dim vc = Mid(line_to_speak, 1, 1) <- you dont need this
    
                Select Case vc  <- you dont need this
                    Case Is = "/" <- you dont need this
                        voice_index = Val(Mid(line_to_speak, 2, 2)) <- you dont need this
                        srate = Val(Mid(line_to_speak, 5, 2)) <- you dont need this
                        edassistv.lstVoices.SelectedIndex = voice_index <- you dont need this
                        selected_voice = edassistv.lstVoices.SelectedItem <- you dont need this
                    Case Else<- you dont need this
                        synth.SelectVoice(selected_voice)
                        synth.Speak(line_to_speak)
                End Select<- you dont need this
    
            Loop
        Catch ex As Exception
            GoTo finish
    
    0 讨论(0)
  • 2021-01-02 05:28

    Use this code...Working for any file .. just send the string to speech function..

    private void speech(String charSequence) {
    
        int position ;
    
    
        int sizeOfChar= charSequence.length();
        String testStri= charSequence.substring(position,sizeOfChar);
    
    
        int next = 20;
        int pos =0;
        while(true) {
            String temp="";
            Log.e("in loop", "" + pos);
    
            try {
    
          temp = testStri.substring(pos, next);
                HashMap<String, String> params = new HashMap<String, String>();
                params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, temp);
                engine.speak(temp, TextToSpeech.QUEUE_ADD, params);
    
                pos = pos + 20;
                next = next + 20;
    
            } catch (Exception e) {
                temp = testStri.substring(pos, testStri.length());
                engine.speak(temp, TextToSpeech.QUEUE_ADD, null);
                break;
    
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2021-01-02 05:34

    In case someone might find this helpful. When you split the large text into strings, do not set the length of each string to the exact value of getMaxSpeechInputLength(). Subtract the string length by 1. Otherwise, only the last chunk of string could be read by TTS.

    int length = toSpeech.getMaxSpeechInputLength() - 1;
    Iterable<String> chunks = Splitter.fixedLength(length).split(largeText);
    Lists.newArrayList(chunks);
    
    0 讨论(0)
提交回复
热议问题