Android:如何将TTS放入AsyncTask中

时间:2013-12-09 12:51:42

标签: android android-asynctask text-to-speech progressdialog

我已经工作了很长时间但仍然坚持下去。 游戏有一个TTS,它似乎需要很长时间才能加载(或实际上是TTS正确的代码?),所以考虑到等待时间很长(在真实设备上测试时大约3秒),我会喜欢放置进度对话框(PD)。

PD有一个循环圈。我按照以下代码编写TTS部分:

的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);  
            }  
        }     
    }    
}

在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();            
    }

试验:

我尝试用doInBackground替换Thread.sleep(1000);中的代码。 PD完美地用于循环循环。

问题:

PD显示但是被冻结,即。圆圈不循环。 我想问一下

  1. 是否应以这种方式编码TTS(实际上并不需要AsyncTask?)

  2. 如果需要AsyncTask,如何重新排列代码?

  3. 我用谷歌搜索但似乎没有人把TTS放在AsyncTask里面。想听听你的意见......非常感谢!

0 个答案:

没有答案