我的代码如下,安卓端能正常播放,但是监听播放结束事件没法触发
textToSpeech(text = '') {
let TextToSpeech = plus.android.importClass("android.speech.tts.TextToSpeech");
let Locale = plus.android.importClass("java.util.Locale");
let Context = plus.android.importClass("android.content.Context");
let mainActivity = plus.android.runtimeMainActivity();
// 实现 TextToSpeech.OnInitListener 接口
let ttsListener = plus.android.implements("android.speech.tts.TextToSpeech$OnInitListener", {
onInit: function(status) {
console.log("TTS onInit called with status: " + status);
if (status != TextToSpeech.ERROR) {
let lang = new Locale("zh", "CN");
if (tts.setLanguage(lang) == TextToSpeech.LANG_MISSING_DATA ||
tts.setLanguage(lang) == TextToSpeech.LANG_NOT_SUPPORTED) {
console.error("所选语言不支持!");
} else {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
}
} else {
console.error("TTS 初始化失败!");
}
}
});
// 创建 TextToSpeech 实例,并传递 OnInitListener
let tts = new TextToSpeech(mainActivity, ttsListener);
// 检查 tts 是否为 null,以确保它已被正确初始化
if (tts) {
console.log("TextToSpeech 实例创建成功");
} else {
console.error("创建 TextToSpeech 实例失败");
}
var UtteranceProgressListener = plus.android.importClass(
"android.speech.tts.UtteranceProgressListener");
let listener = new UtteranceProgressListener({
onStart: function() {
console.log('start')
},
onDone: function() {
console.log('onDone')
},
onError: function() {
console.log('onError')
}
})
tts.setOnUtteranceProgressListener(listener)
}
1 个回复
9***@qq.com
楼主你好,请问你解决这个问题了吗?