10

I am developing android application in which I am using text to speech conversion.What I need when I open my application run text to speech conversion. After completion of this I want to do some thing.My code looks like

public class Mainactivity extends Activity implements OnInitListener, OnUtteranceCompletedListener{
    private static int REQ_CODE = 1;
    private TextToSpeech tts = null;
    private boolean ttsIsInit = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    startTextToSpeech();
    }

    private void startTextToSpeech() {
        Intent intent = new Intent(Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(intent, REQ_CODE);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQ_CODE) {
            if (resultCode == Engine.CHECK_VOICE_DATA_PASS) {
                tts = new TextToSpeech(this, this); 
            } 
            else {
                Intent installVoice = new Intent(Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installVoice);
            }
        }
    }

        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                ttsIsInit = true;
                int result = tts.setOnUtteranceCompletedListener(this);
                if (tts.isLanguageAvailable(Locale.ENGLISH) >= 0)
                    tts.setLanguage(Locale.ENGLISH);
                tts.setPitch(5.0f);
                tts.setSpeechRate(1.0f);

                 HashMap<String, String> myHashAlarm = new HashMap<String, String>();
                  myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
                  myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "SOME MESSAGE");
                  tts.speak("hi how are you?", TextToSpeech.QUEUE_FLUSH, myHashAlarm);
             }
        }

   @Override
   public void onDestroy() {
      if (tts != null) {
        tts.stop();
        tts.shutdown();
      }
        super.onDestroy();
     }

   @Override
   public void onUtteranceCompleted(String uttId) {
       Toast.makeText(Mainactivity.this,"done", Toast.LENGTH_LONG).show();
       if (uttId.equalsIgnoreCase("done")) {
           Toast.makeText(Mainactivity.this,"inside done", Toast.LENGTH_LONG).show();
       } 
   }
}

When I open my application text to speech working fine. But how to detect whether text to speech completed or not.Need help..... Thank you.....

4

3 に答える 3

10

API レベル 15 以降を使用している場合は、TextToSpeech参照に進捗リスナーを設定できます。

setOnUtteranceProgressListener(UtteranceProgressListener listener)

終了時のコールバックを含む、TTS の進行状況を報告するコールバックを受け取ります。http://developer.android.com/reference/android/speech/tts/TextToSpeech.htmlおよびhttp://developer.android.com/reference/android/speech/tts/UtteranceProgressListener.htmlを参照してください。

ただし、既に deprecated を使用していることに気付きましたOnUtteranceCompletedListener。コールバックを取得していますonUtteranceCompleted()か? それもうまくいくはずです。

于 2012-07-10T08:54:44.777 に答える
8

tts オブジェクトの onInit 関数内で setOnUtteranceCompletedListener を呼び出します。

onUtteranceCompleted 関数の呼び出し時に UI を変更する場合は、runOnUIThread メソッド内にコードを追加します。

そして、speak() 関数を呼び出すときに Hashmap パラメータ値を追加することを忘れないでください

例 :

TextToSpeech tts= new TextToSpeech(context, new OnInitListener() {

 @Override
 public void onInit(int status) {

    mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {

        @Override
        public void onUtteranceCompleted(String utteranceId) {

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                //UI changes
                }
            });
        }
    });

 }
});


HashMap<String, String> params = new HashMap<String, String>();

params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId");

tts.speak("Text to Speak",TextToSpeech.QUEUE_FLUSH, params);
于 2013-03-14T07:17:51.770 に答える
4

Here is some code from here that helps you be backward compatible so you don't have to target 15.

private void setTtsListener()
    {
        final SpeechRecognizingAndSpeakingActivity callWithResult = this;
        if (Build.VERSION.SDK_INT >= 15)
        {
            int listenerResult = tts.setOnUtteranceProgressListener(new UtteranceProgressListener()
            {
                @Override
                public void onDone(String utteranceId)
                {
                    callWithResult.onDone(utteranceId);
                }

                @Override
                public void onError(String utteranceId)
                {
                    callWithResult.onError(utteranceId);
                }

                @Override
                public void onStart(String utteranceId)
                {
                    callWithResult.onStart(utteranceId);
                }
            });
            if (listenerResult != TextToSpeech.SUCCESS)
            {
                Log.e(TAG, "failed to add utterance progress listener");
            }
        }
        else
        {
            int listenerResult = tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener()
            {
                @Override
                public void onUtteranceCompleted(String utteranceId)
                {
                    callWithResult.onDone(utteranceId);
                }
            });
            if (listenerResult != TextToSpeech.SUCCESS)
            {
                Log.e(TAG, "failed to add utterance completed listener");
            }
        }
    }
于 2012-07-10T15:18:31.013 に答える