代码之家  ›  专栏  ›  技术社区  ›  Ranjit Vamadevan

setOnTutteranceProgressListener对于API>21的文本到语音根本不起作用

  •  1
  • Ranjit Vamadevan  · 技术社区  · 7 年前

    setOnUtteranceProgressListener 应该通知 Toast 设置会话进程侦听器 paramaters 如下所示。。

        Bundle params = new Bundle();
        params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, MainActivity.this.getPackageName());
    

    我在调用speak函数时给出了一个“UniqueId”,如下所示。

        myTTS.speak(message,TextToSpeech.QUEUE_FLUSH,params,"UniqueId");
    

    在我的 program setOnUtteranceProgressListner 好像不行。

        myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
            @Override
            public void onStart(String utteranceId) {
    
            }
    
            @Override
            public void onDone(String utteranceId) {
    
                Toast.makeText(MainActivity.this,"Finished speaking.",Toast.LENGTH_LONG).show();
            }
    
            @Override
            public void onError(String utteranceId) {
    
            }
        });
    

    所有代码如下。。

        public class MainActivity extends AppCompatActivity {
    String message;
    private TextToSpeech myTTS;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        myTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(myTTS.getEngines().size() == 0){
                    Toast.makeText(MainActivity.this,"No Engines Installed",Toast.LENGTH_LONG).show();
                }else{
                    myTTS.setLanguage(Locale.US);
    
                    if (status == TextToSpeech.SUCCESS){
                        //Toast.makeText(MainActivity.this,"Status working.",Toast.LENGTH_LONG).show();
                        message = "How may i help you.";
                    }
    
                }
            }
        });
    
        myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
            @Override
            public void onStart(String utteranceId) {
    
            }
    
            @Override
            public void onDone(String utteranceId) {
    
                Toast.makeText(MainActivity.this,"onDone working.",Toast.LENGTH_LONG).show();
            }
    
            @Override
            public void onError(String utteranceId) {
    
            }
        });
    }
    

    请给出解决办法。

    1 回复  |  直到 7 年前
        1
  •  10
  •   Nerdy Bunz    7 年前

    1) 在初始化tts之前设置进度侦听器。

    2) 试着用背景线干杯。

    public class MainActivity extends AppCompatActivity {
        String message = "How may I help you?";
        String mostRecentUtteranceID;
        private TextToSpeech myTTS;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            myTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    if(myTTS.getEngines().size() == 0){
                        Toast.makeText(MainActivity.this,"No Engines Installed",Toast.LENGTH_LONG).show();
                    }else{
                        if (status == TextToSpeech.SUCCESS){
                            ttsInitialized();
                        }
                    }
                }
            });
        }
    
        private void ttsInitialized() {
    
            // *** set UtteranceProgressListener AFTER tts is initialized ***
            myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                @Override
                public void onStart(String utteranceId) {
    
                }
    
                @Override
                // this method will always called from a background thread.
                public void onDone(String utteranceId) {
    
                    // only respond to the most recent utterance
                    if (!utteranceId.equals(mostRecentUtteranceID)) { 
                        Log.i("XXX", "onDone() blocked: utterance ID mismatch.");
                        return; 
                    } // else continue...
    
                    boolean wasCalledFromBackgroundThread = (Thread.currentThread().getId() != 1);
                    Log.i("XXX", "was onDone() called on a background thread? : " + wasCalledFromBackgroundThread);
    
                    Log.i("XXX", "onDone working.");
    
                    // for demonstration only... avoid references to 
                    // MainActivity (unless you use a WeakReference)
                    // inside the onDone() method, as it
                    // can cause a memory leak.
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // *** toast will not work if called from a background thread ***
                            Toast.makeText(MainActivity.this,"onDone working.",Toast.LENGTH_LONG).show();
                        }
                    });
                }
    
                @Override
                public void onError(String utteranceId) {
    
                }
            });
    
            // set Language
            myTTS.setLanguage(Locale.US);
    
            // set unique utterance ID for each utterance
            mostRecentUtteranceID = (new Random().nextInt() % 9999999) + ""; // "" is String force
    
            // set params
            // *** this method will work for more devices: API 19+ ***
            HashMap<String, String> params = new HashMap<>();
            params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, mostRecentUtteranceID);
    
            myTTS.speak(message,TextToSpeech.QUEUE_FLUSH,params);
    
        }
    
    }