0
 public boolean playing=false;
  private void prepareTTSEngine() {
        try {
            synthesis = SpeechSynthesis.getInstance(this);


            synthesis.setSpeechSynthesisEvent(new SpeechSynthesisEvent() {

                public void onPlaySuccessful() {
                    Log.i(TAG, "onPlaySuccessful");
                    playing=true;

                }

                public void onPlayStopped() {
                    Log.i(TAG, "onPlayStopped");
                    playing=false;

                }

                public void onPlayFailed(Exception e) {
                    Log.e(TAG, "onPlayFailed");
                    e.printStackTrace();
                    playing=false;
                }

                public void onPlayStart() {
                    Log.i(TAG, "onPlayStart");
                    //playing=true;

                }

                @Override
                public void onPlayCanceled() {
                    Log.i(TAG, "onPlayCanceled");
                    playing=false;
                }


            });

            //synthesis.setVoiceType("usenglishfemale1"); // All the values available to you can be found in the developer portal under your account

        } catch (InvalidApiKeyException e) {
            Log.e(TAG, "Invalid API key\n" + e.getStackTrace());
            Toast.makeText(_context, "ERROR: Invalid API key", Toast.LENGTH_LONG).show();
        }

    }

         prepareTTSEngine();
        String speech= MapLocationOverlay.mSelectedMapLocation.getShortDesc();
        synthesis.setVoiceType("usenglishfemale");

        BreakIterator iterator = BreakIterator.getSentenceInstance(Locale.US);
        iterator.setText(speech);
        int start = iterator.first();

        for (int end = iterator.next();end != BreakIterator.DONE; start = end, end = iterator.next()) {
            if(speech !=null && speech.substring(start,end).compareTo("")!=0)
            {
                String sentence_next=speech.substring(start,end);                   
                        //set ready flag to true (so isReady returns true) 


                while(playing==true)
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                            try {
                                synthesis.speak(sentence_next);
                            } catch (BusyException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (NoNetworkException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }


enter code here


            }
        }
    }

I'm using [the iSpeech library'sSpeechSynthesisは、一度に 1 つの文を渡し、すべての文を一度に開始する for`-loop をclass](http://www.ispeech.org/androidsdkdoc/com/ispeech/SpeechSynthesis.html). When I call the話します。method in aこれは、メソッドがノンブロッキングであるためだと思います。オープンソースではないので、どのように機能するかわかりません。前の文が完了するまで、1 つの文を開始しないようにするにはどうすればよいですか?

4

1 に答える 1

2

通常、この種のライブラリでは、コードはサウンドの再生が停止するのを待ちません (正当な理由により)。

ライブラリのドキュメントから、次の文を読む前にSpeechSynthesisEventwithを聞く必要があるようです。PLAY_SUCCESSFUL

必要に応じてjavax.speech.synthesis、スピーチが完了するまで待機するかなり単純なメカニズムがある場合は、http: //java.sun.com/products/java-media/speech/forDevelopers/jsapi-guide/Synthesis.htmlを確認してください。

于 2012-03-17T13:56:18.120 に答える