文本到语音如何在UtteranceProgressListener.onRange()上回调

时间:2018-06-12 06:10:47

标签: android text-to-speech

我正在尝试回复onRange()文字转语音UtteranceProgressListener。目前我在onStart()onEnd()上收到回调。但我无法在onRange().上收到回调

任何人都可以通过onRange()帮助回复。检查了示例代码但无法找到合适的解决方案。

我想我必须使用SynthesisCallback.rangeStart(),但我无法获得任何示例代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = findViewById(R.id.tv);
    tv.setText("Hello how are you");
    Intent ttsInstallCheck = new Intent();
    ttsInstallCheck.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(ttsInstallCheck, TTS_REQUEST_CODE);

}
@Override
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {

        int result = t1.setLanguage(Locale.US);

        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "This Language is not supported");
        } else {
            speakOut();
        }

    } else {
        Log.e("TTS", "Initilization Failed!");
    }
}

private void speakOut() {
    String text = tv.getText().toString();
    HashMap<String, String> params = new HashMap<String, String>();
    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, text);
    t1.speak(text, TextToSpeech.QUEUE_FLUSH, params);

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (TTS_REQUEST_CODE == requestCode) {
        if (TextToSpeech.Engine.CHECK_VOICE_DATA_PASS == resultCode) {
            t1 = new TextToSpeech(this, this);
            t1.setOnUtteranceProgressListener(new UtteranceProgressListener() {

                @Override
                public void onStart(final String utteranceId) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(MainActivity.this, utteranceId, Toast.LENGTH_SHORT).show();
                        }
                    });

                }

                @Override
                public void onError(String utteranceId) {                       
                }

                @Override
                public void onDone(String utteranceId) {                       
                }

                @Override
                public void onRangeStart(String utteranceId, int start, int end, int frame) {
                    super.onRangeStart(utteranceId, start, end, frame);
                    Toast.makeText(MainActivity.this, "" + start, Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            // not installed
        }
    }
}
}

0 个答案:

没有答案