UtteranceProgressListener不适用于Android TTS

ztr*_*nge 5 java android text-to-speech

我正在制作一个应用程序,每次触发广播接收器时都使用tts来合成wav文件.我正在开发AndroidStudio(最新版)并使用最低15级的API级别19.

我有一个带有BroadcastListener的服务.每次执行BroadcastListener onReceive方法时,我都会使用

tts.synthesizeToFile(playing, null, storagePath + "/" + "tst.wav");

该文件已成功创建,但永远不会调用UtteranceProgressListener的onDone()方法.

public void onCreate() {
    super.onCreate();

    //Get TTS capabilities
    //TODO: Use TextToSpeech.Engine.ACTION_CHECK_TTS_DATA to check if tts is available
    tts = new TextToSpeech(PresenterService.this,
            new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {

                    //If the TTS engine was started successfully

                    if (status != TextToSpeech.ERROR) {

                        tts.setLanguage(Locale.US);
                        tts.setPitch(PRESENTER_PITCH);
                        tts.setSpeechRate(PRESENTER_RATE);
                    }

                }
            });

    tts.setOnUtteranceProgressListener(new TtsUtteranceListener());



    IntentFilter filter = new IntentFilter();
    filter.addAction("xxxxxxxxxxxxxxxxxx");

    mReceiver = new TrackChangedReceiver(tts);
    registerReceiver(mReceiver, filter);

}
Run Code Online (Sandbox Code Playgroud)

和TtsUtteranceListener类:

public class TtsUtteranceListener extends UtteranceProgressListener {

    @Override
    public void onDone(String utteranceId) {
        Log.d("TtsUtteranceListener", "utterance Done: " + utteranceId);
    }

    @Override
    public void onStart(String utteranceId) {
        Log.d("TtsUtteranceListener", "utterance Start: " + utteranceId);
    }

    @Override
    public void onError(String utteranceId) {
        Log.d("TtsUtteranceListener", "utterance Error: " + utteranceId);
    }
}
Run Code Online (Sandbox Code Playgroud)

听众的方法:

public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (action != null) {
        if (action.equalsIgnoreCase("xxxxxxxxxxxxxxxxx")) {
            String playing = "test string";
            tts.synthesizeToFile(playing, null, storagePath + "/" + "tst.wav");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用AndroidStudio,并且在调试模式下我可以看到tts对象的mUtteranceProgressListener有一些对象引用:

com.example.android.ttstest.TtsUtteranceListener@41eaf8d8
Run Code Online (Sandbox Code Playgroud)

但是从不调用监听器的方法.Log.d()调用永远不会工作,并且永远不会触发任何断点.

我也试过在调用时将UtteranceProgressListener声明为匿名类

tts.setOnUtteranceProgressListener(new UtteranceProgressListener(){...});
Run Code Online (Sandbox Code Playgroud)

但同样的事情......

有人能指出我做错了什么吗?

ztr*_*nge 8

好的,我解决了.问题是我没能通过KEY_PARAM_UTTERANCE_ID.

if (action.equalsIgnoreCase("xxxxxxxxxxxxxxxxx")) {
    HashMap<String, String> hashTts = new HashMap<String, String>();
    hashTts.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "id");

    String playing = "test string";
    tts.synthesizeToFile(playing, hashTts, storagePath + "/" + "tst.wav");
}
Run Code Online (Sandbox Code Playgroud)

我将参数参数传递为null:

tts.synthesizeToFile(playing, null, storagePath + "/" + "tst.wav");
Run Code Online (Sandbox Code Playgroud)