Search code examples
androidstringandroid-asynctaskmedia-playergoogle-api

Controlling input for Google TTS API


I'm creating a TTS app using google's unofficial tts api and it works fine but the api can only process a maximum of 100 characters at a time but my application may have to send strings containing as many as 300 characters.

Here is my code

        try {
        String text = "bonjour comment allez-vous faire";

        text=text.replace(" ", "%20"); 
        String oLanguage="fr";

        MediaPlayer player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);           
        player.setDataSource("http://translate.google.com/translate_tts?tl=" + oLanguage + "&q=" + text);

        player.prepare();
        player.start(); 

    } catch (Exception e) { 
        // TODO: handle exception
    }

So my questions are

  1. How do I get it to check the number of characters in the string and send only complete words within the 100 character limit.

  2. How do I detect when the first group of TTS is finished so I can send the second to avoid both speech overlapping each other

  3. Is there any need for me to use Asynctask for this process?


Solution

  • 1.How do I get it to check the number of characters in the string and send only complete words within the 100 character limit.

        ArrayList<String> arr = new ArrayList<String>();
        int counter = 0;
        String textToSpeach = "Your long text";
        if(textToSpeach.length()>100)
        {   
            for(int i =0 ; i<(textToSpeach.length()/100)+1;i++)
            {
            String temp = textToSpeach.substring(0+counter,99+counter);
            arr.add(temp.substring(0, temp.lastIndexOf(" ")));
            counter = counter + 100;
            }
        }
    

    2.How do I detect when the first group of TTS is finished so I can send the second to avoid both speech overlapping each other

        player.setOnCompletionListener(new OnCompletionListener() {
    
            @Override
            public void onCompletion(MediaPlayer mp) {
    
                // pass next block
            }
        });
    

    3.Is there any need for me to use Asynctask for this process?

    Right now I dont see any need for that.