10

我正在开发我正在使用文本到语音转换的 android 应用程序。当我打开我的应用程序运行文本到语音转换时我需要什么。完成后我想做一些事情。我的代码看起来像

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

当我打开我的应用程序文本到语音工作正常。但是如何检测文本到语音是否完成。需要帮助......谢谢......

4

3 回答 3

10

如果您使用 API 级别 15 或更高版本,您可以使用在您的TextToSpeech参考上设置进度侦听器

setOnUtteranceProgressListener(UtteranceProgressListener listener)

您将收到报告 TTS 进度的回调,包括完成时的回调。请参阅http://developer.android.com/reference/android/speech/tts/TextToSpeech.htmlhttp://developer.android.com/reference/android/speech/tts/UtteranceProgressListener.html

但是,我注意到您已经在使用已弃用的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

这里有一些代码可以帮助您向后兼容因此您不必以 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 回答