【问题标题】:Android TTS onUtteranceCompleted callback isn't getting called未调用 Android TTS onUtteranceCompleted 回调
【发布时间】:2011-06-06 21:09:10
【问题描述】:

我试图让 Android TTS API 读取我的“话语”,然后调用 onUtteranceCompleted() 侦听器失败。我已经注册了我的 TTS 对象并且它返回了 SUCCESS,所以我一生都无法弄清楚为什么我的回调没有被调用。

我尝试过寻求帮助,但似乎其他人也遇到了困难。我错过了一些简单的东西吗?

感谢您提供的任何帮助。

package com.test.mytts;

import java.util.HashMap;

import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.widget.TextView;
import android.widget.Toast;

public class MyTTS extends Activity implements OnInitListener, OnUtteranceCompletedListener
{   
    TextView tv;
    private TextToSpeech _tts;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        tv = new TextView(this);

        tv.setText("MyTTS: ");

        super.onCreate(savedInstanceState);
        setContentView(tv);

        _tts = new TextToSpeech(this, this);
    }

    @Override
    public void onInit(int status) 
    {
        HashMap<String, String> myHashAlarm = new HashMap<String, String>();

        myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
        myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "test");

        if (status == TextToSpeech.SUCCESS)
        {
            Toast.makeText(this, "Trying to speak...", Toast.LENGTH_SHORT).show();

            int result = _tts.setOnUtteranceCompletedListener(this);

            tv.append(String.valueOf(result));

            _tts.setSpeechRate((float) .5);

            _tts.speak("Testing one, two, three", TextToSpeech.QUEUE_ADD, myHashAlarm);
        }
        else
            Toast.makeText(this, "Failed to initialize TTS.", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onUtteranceCompleted(String utteranceId) 
    {
        Toast.makeText(this, "onUtteranceCompleted", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        _tts.shutdown();
    }
}

【问题讨论】:

标签: android text-to-speech


【解决方案1】:

调用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);

【讨论】:

    【解决方案2】:

    我相信,除非您指定带有 id 的话语,例如:

    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceid);
    

    不会调用您的话语完成方法。

    在这种情况下,map 是你说话时传递给引擎的 Hashmap。

    【讨论】:

    • 遇到与上述相同的问题。而且我确实将这个键添加到了地图中并在说话时传递了它。奇怪...
    • 我通常在每次调用 speak 时指定监听器。可能会有所帮助。
    • 不知何故我现在开始工作了。但不知道为什么它现在可以工作。我猜有些奇怪。
    • 同样的问题,我有那个唯一的密钥但不能工作?也许我需要任何许可。安卓 6.0.1
    【解决方案3】:

    这将适用于 API 级别 >=15

    import java.util.HashMap;
    import java.util.Locale;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.speech.tts.TextToSpeech;
    import android.speech.tts.TextToSpeech.OnInitListener;
    import android.speech.tts.UtteranceProgressListener;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements OnInitListener{
    
        private static final int CHECK_TTS_DATA = 0X123;
        protected static final String TAG = MainActivity.class.getSimpleName();
        private TextToSpeech textToSpeech;
        private Button buttonSayIt;
        private EditText editTextTts;
        String tts;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            buttonSayIt=(Button) findViewById(R.id.buttonSayIt);
            editTextTts=(EditText) findViewById(R.id.editTextTts);
            buttonSayIt.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    tts=editTextTts.getText().toString();
                    Log.d(TAG, tts);
                    speach(tts,"you_utterance_id");
                }
            });
            //check for TTs data
            Intent checkTtsDataIntent=new Intent();
            checkTtsDataIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
            startActivityForResult(checkTtsDataIntent, CHECK_TTS_DATA);
    
        }
    
        protected void speach(String tts,String utteranceId) {
            HashMap<String, String> params = new HashMap<String, String>();
            params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,utteranceId);
            textToSpeech.speak(tts,TextToSpeech.QUEUE_FLUSH,params);
        }
    
        @Override
        public void onInit(int status) {
            if(status==TextToSpeech.SUCCESS){
                if(textToSpeech.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE){
                    textToSpeech.setLanguage(Locale.US);
                }
            }else if(status==TextToSpeech.ERROR){
                Toast.makeText(this, "Sorry Text To Speach faild", Toast.LENGTH_SHORT).show();
            }
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(requestCode==CHECK_TTS_DATA){
                if(resultCode==TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
                    textToSpeech=new TextToSpeech(this, this);      
                    textToSpeech.setOnUtteranceProgressListener(utteranceProgressListener);
                }else{
                    Intent installTtsIntent=new Intent();
                    installTtsIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                    startActivity(installTtsIntent);
                }
            }
        }
    
        UtteranceProgressListener utteranceProgressListener=new UtteranceProgressListener() {
    
            @Override
            public void onStart(String utteranceId) {
                Log.d(TAG, "onStart ( utteranceId :"+utteranceId+" ) ");
            }
    
            @Override
            public void onError(String utteranceId) {
                Log.d(TAG, "onError ( utteranceId :"+utteranceId+" ) ");
            }
    
            @Override
            public void onDone(String utteranceId) {
                Log.d(TAG, "onDone ( utteranceId :"+utteranceId+" ) ");
            }
        };
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }
    

    【讨论】:

      【解决方案4】:

      如果有人仍然觉得困难

      代码片段

      textToSpeech=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
          @Override
          public void onInit(int status) {
              if (status==TextToSpeech.SUCCESS){
                  int result=textToSpeech.setLanguage(Locale.ENGLISH);
      
                  if (result==TextToSpeech.LANG_MISSING_DATA||result==TextToSpeech.LANG_NOT_SUPPORTED){
                      Log.i("TextToSpeech","Language Not Supported");
                  }
      
                  textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                      @Override
                      public void onStart(String utteranceId) {
                          Log.i("TextToSpeech","On Start");
                      }
      
                      @Override
                      public void onDone(String utteranceId) {
                          Log.i("TextToSpeech","On Done");
                      }
      
                      @Override
                      public void onError(String utteranceId) {
                          Log.i("TextToSpeech","On Error");
                      }
                  });
      
              }else {
                  Log.i("TextToSpeech","Initialization Failed");
              }
          }
      });
      
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
              textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null,TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-21
        • 2013-12-16
        • 1970-01-01
        • 1970-01-01
        • 2014-09-25
        • 1970-01-01
        • 1970-01-01
        • 2017-05-10
        相关资源
        最近更新 更多