2011-01-25 72 views
4

我真的在努力尋找某些東西......我想要閱讀幾個句子,都是通過tts speek函數口頭閱讀,並通過屏幕上的文本,一個一次一句。Android:等到tts發言完成,然後繼續

我已經準備好了textview區域,但把它放在一起是我沒有得到。它可以讀取所有的句子,只顯示最後一個句子,或者只顯示並閱讀第一句。

任何人都知道我該如何實現這一目標?

回答

-1
boolean speakingEnd = tts.isSpeaking(); 
do{ 
    speakingEnd = tts.isSpeaking(); 
} while (speakingEnd); 
//Continue with code 
1
public void speak(String message){   
    tts.speak(message, TextToSpeech.QUEUE_FLUSH, null); 
    while (tts.isSpeaking()){ 
     System.Out.Println("Do something or nothing while speaking..") 
    } 
} 
3

我只是碰到了這個問題,根據speak method,使用UtteranceProgressListener。我發現這不會在UI線程上執行,所以我不得不使用runOnUiThread()來更新活動。

tts.setOnUtteranceProgressListener(new UtteranceProgressListener() { 
     @Override 
     public void onStart(String utteranceId) { 

     } 

     @Override 
     public void onDone(String utteranceId) { 
      LettersActivity.this.runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        // Do something on UI thread 
       } 
      }); 
     } 

     @Override 
     public void onError(String utteranceId) { 
      Log.e(TAG, "error on " + utteranceId); 
     } 
    }); 
+0

謝謝,但只有運行API> = 15 – mesutpiskin 2017-03-12 00:09:07

0

試試這個

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{ 


    private boolean initialized; 
    private String queuedText; 
    private String TAG = "TTS"; 
    private TextToSpeech tts; 




    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 



     tts = new TextToSpeech(this /* context */, this /* listener */); 
     tts.setOnUtteranceProgressListener(mProgressListener); 


     speak("hello world"); 

    } 




    public void speak(String text) { 

     if (!initialized) { 
      queuedText = text; 
      return; 
     } 
     queuedText = null; 

     setTtsListener(); // no longer creates a new UtteranceProgressListener each time 
     HashMap<String, String> map = new HashMap<String, String>(); 
     map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId"); 
     tts.speak(text, TextToSpeech.QUEUE_ADD, map); 
    } 


    private void setTtsListener() { 

    } 





    @Override 
    public void onInit(int status) { 
     if (status == TextToSpeech.SUCCESS) { 
      initialized = true; 
      tts.setLanguage(Locale.ENGLISH); 

      if (queuedText != null) { 
       speak(queuedText); 
      } 
     } 
    } 



    private abstract class runnable implements Runnable { 
    } 




    private UtteranceProgressListener mProgressListener = new UtteranceProgressListener() { 
     @Override 
     public void onStart(String utteranceId) { 
     } // Do nothing 

     @Override 
     public void onError(String utteranceId) { 
     } // Do nothing. 

     @Override 
     public void onDone(String utteranceId) { 

      new Thread() 
      { 
       public void run() 
       { 
        MainActivity.this.runOnUiThread(new runnable() 
        { 
         public void run() 
         { 

          Toast.makeText(getBaseContext(), "TTS Completed", Toast.LENGTH_SHORT).show(); 

         } 
        }); 
       } 
      }.start(); 

     } 
    }; 


} 
相關問題