Android: how to put TTS inside AsyncTask

怎甘沉沦 提交于 2019-12-13 20:04:19

问题


I have been working for so long but still stucked. The game has a TTS and it appears to require a long time to load (or actually is the code for the TTS correct??), so in view of the long waiting time (about 3 second when testing on real device), I would like to put a Progress Dialog (PD).

The PD has a looping circle. I code the TTS part as in the following code:

AsyncTask:

private class MainFrameTask extends AsyncTask<String,Integer,String> implements OnInitListener, OnUtteranceCompletedListener
{
    private Index_game_card_intro mainFrame = null;  

    public MainFrameTask(Index_game_card_intro mainFrame)
    {  
        this.mainFrame = mainFrame;  
    }  

    @Override  
    protected void onCancelled() 
    {  
        stopProgressDialog();  
        super.onCancelled();  
    }  

    @Override
    protected void onPreExecute()
    {           
        startProgressDialog(); 
    }

    @Override
    protected String doInBackground(String... params) 
    {            
        // setup TTS part 1.1
          mTts = new TextToSpeech(Index_game_card_intro.this, this);  // TextToSpeech.OnInitListener



        return "Done!";
    }

    protected void onPostExecute(String result) 
    {
        stopProgressDialog();      
    }

 // setup TTS part 2    
    @Override
    public void onUtteranceCompleted(String utteranceId) 
    {  
        Log.v(TAG, "Get completed message for the utteranceId " + utteranceId);  
        lastUtterance = Integer.parseInt(utteranceId);  
    }  

// setup TTS part 3 
    @Override
    public void onInit(int status) 
    {  
        if(status == TextToSpeech.SUCCESS)  
        {  
            int result = mTts.setLanguage(Locale.US);  // <====== set speech location
            mTts.setSpeechRate((float) 0.8);
            mTts.setPitch(1.0f);
            if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)  
            {  
                // button_header.setEnabled(false);  
            }  
            else  
            {  
                // button_header.setEnabled(true);  
                mTts.setOnUtteranceCompletedListener(this);  
            }  
        }     
    }    
}

Outside AsyncTask:

// setup TTS part 4 
    private void speakText()  
    {  
        lastUtterance++;  
        if(lastUtterance >= loveArray.length)  
        {  
            lastUtterance = 0;  
        }  
        Log.v(TAG, "the begin utterance is " + lastUtterance);  
        for(int i = lastUtterance; i < loveArray.length; i++)  
        {  
            params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, String.valueOf(i));  
            mTts.speak(loveArray[i], TextToSpeech.QUEUE_ADD, params);  
            mTts.playSilence(ttsilience, TextToSpeech.QUEUE_ADD, null);
        }  
    } 

    public void speak_action(String to_speak) 
    {
        // speaking
        // setup TTS part 1.2
        StringTokenizer loveTokens = new StringTokenizer(to_speak,",.");  
        int s = 0;  
        loveArray = new String[loveTokens.countTokens()];  
        while(loveTokens.hasMoreTokens())  
        {  
            loveArray[s++] = loveTokens.nextToken();  
        }  
        speakText();            
    }

Trial:

I have tried to replace the code inside doInBackground with Thread.sleep(1000);. The PD works with circle looping perfectly.

Question:

The PD is shown but is freezed, ie. the circle does not loop. I would like to ask

  1. whether TTS should be coded in this way (which does not actually requires AsyncTask?)

  2. if need AsyncTask, how could the codes be rearranged?

I have googled but seem there is no one putting TTS inside AsyncTask. Would like to hear your advice... Thanks so much!

来源:https://stackoverflow.com/questions/20471159/android-how-to-put-tts-inside-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!