// import { TSpeakInitParams, TSpeakResult } from "../interface.uts"
import TextToSpeech from "android.speech.tts.TextToSpeech";
import Locale from "java.util.Locale";
import Context from "android.content.Context";
import { TSpeakInitParams, TSpeakResult } from '../interface.uts'
export class RockTtsSpeaker {
tts : TextToSpeech | null = null;
constructor() {
// this.tts = this.init({}, (e) => {
// console.log(99999, 111)
// })
}
init(params : TSpeakInitParams, callback : (res : TSpeakResult) => void) : TextToSpeech {
const context = UTSAndroid.getUniActivity()!;
var speaker : TextToSpeech | null = null
speaker = TextToSpeech(context, TextToSpeech.OnInitListener({
onInit(status : UInt) {
if (status == TextToSpeech.SUCCESS as UInt) {
// 设置语言为中文
const result = speaker?.setLanguage(Locale.CHINA);
if (result === TextToSpeech.LANG_MISSING_DATA || result === TextToSpeech.LANG_NOT_SUPPORTED) {
callback({ success: status, message: 'TTS 不支持中文语音' });
} else {
callback({ success: status, message: '初始化成功' });
}
callback({ success: status, message: '初始化成功' });
} else {
// callback({ success: Number.from(status), message: '初始化失败' });
callback({ success: status, message: '初始化失败' });
}
}
}));
this.tts = speaker
return speaker
}
}
interface.uts
/* 同步函数定义 */
export type MyApiSync = (paramA : boolean) => MyApiResult
export type TSpeakResult = {
success : UInt
message : string
}
export type TSpeakInitParams = {
lang ?: 'ZH' | string
country ?: 'CN' | string
speechRate?:UInt
}