[add] iOS TTS

This commit is contained in:
建喵 2022-08-25 14:25:48 +08:00
parent 33aac332c5
commit 4bee99bfcb
4 changed files with 294 additions and 137 deletions

View File

@ -1,4 +1,4 @@
import Text_to_Speech from "./Text_to_Speech"; import NativeClass from "./NativeClass";
const { ccclass, property } = cc._decorator; const { ccclass, property } = cc._decorator;
@ -16,7 +16,7 @@ export default class Manager extends cc.Component {
//#region private //#region private
private _text_to_Speech: Text_to_Speech; // private _text_to_Speech: Text_to_Speech;
//#endregion //#endregion
@ -26,8 +26,11 @@ export default class Manager extends cc.Component {
if (CC_DEBUG) { if (CC_DEBUG) {
console.log("Debug"); console.log("Debug");
} }
new NativeClass();
let self: this = this; let self: this = this;
this._text_to_Speech = new Text_to_Speech(); // this._text_to_Speech = new Text_to_Speech();
window.addEventListener("message", function (e: MessageEvent<any>): void { window.addEventListener("message", function (e: MessageEvent<any>): void {
let data: any = e.data; let data: any = e.data;
let method: string = data.method; let method: string = data.method;
@ -81,7 +84,8 @@ export default class Manager extends cc.Component {
} }
public Speak(msg: string): void { public Speak(msg: string): void {
this._text_to_Speech.speak(msg); // this._text_to_Speech.speak(msg);
NativeClass.Instance.TTS_Play(msg);
} }
public Alert(msg: string): void { public Alert(msg: string): void {

View File

@ -0,0 +1,194 @@
const { ccclass } = cc._decorator;
@ccclass
export default class NativeClass extends cc.Component {
//#region static 屬性
private static _instance: NativeClass = null;
public static get Instance(): NativeClass { return NativeClass._instance; }
//#endregion
//#region public 屬性
public URLscheme: Object = {};
public URLschemeFlags: number = 0;
//#endregion
//#region Lifecycle
constructor() {
super();
NativeClass._instance = this;
this.init();
}
private init(): void {
let self: this = this;
let HttpData: any[] = [];
// let HttpParmData: any[] = [];
// 註冊要被Java、OC呼叫的事件
if (!window["JSBridge"]) {
window["JSBridge"] = function (method: string = "", value: string = ""): void {
if (method || self[method]) {
if (value) {
self[method](value);
} else if (self[method]) {
self[method]();
} else {
console.log(`not function: ${method}, value: ${value}`);
}
} else {
console.log(`not function: ${method}, value: ${value}`);
}
};
}
if (cc.sys.isBrowser) {
let url: string = window.location.search;
// let request: object = [];
if (url.indexOf("?") !== -1) {
let str: string = url.substring(1);
let strs: string[] = str.split("&");
for (let i: number = 0; i < strs.length; i++) {
this.URLscheme[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
}
}
} else {
switch (cc.sys.os) {
case cc.sys.OS_WINDOWS: {
// cc.sys.platform = 101(DESKTOP_BROWSER)
break;
}
case cc.sys.OS_ANDROID: {
// cc.sys.platform = 3(Android)
// 呼叫Java取得由URL打開時的網址(呼叫getIntent 無帶入參數(null) 回傳字串((Ljava/lang/String;)Ljava/lang/String;))
/** 取得由URL打開時的網址(內容是自己取得後用特殊符號拼成字串) */
let scheme: any = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getURLData", "(Ljava/lang/String;)Ljava/lang/String;", null);
if (scheme && scheme !== "null") {
let _Get_Arr: string[] = scheme.split("?");
let Get_Arr: string[] = _Get_Arr[1].split("#");
for (let i: number = 0; i < Get_Arr.length; i++) {
let Get: string[] = Get_Arr[i].split("$");
HttpData[Get[0]] = Get[1];
}
let Query: string = HttpData["Query"];
if (Query) {
Get_Arr = Query.split("&");
for (let i: number = 0; i < Get_Arr.length; i++) {
let Get: string[] = Get_Arr[i].split("=");
this.URLscheme[Get[0]] = Get[1];
}
}
}
// cc.log("getIntent OK, " + getIntent);
break;
}
case cc.sys.OS_IOS: {
// cc.sys.platform = 4(iPhone)
// 呼叫OC取得由URL打開時的網址(呼叫getIntent 無帶入參數 回傳字串)(開發中功能,預計要取得由URL打開時的網址)
/** 取得由URL打開時的網址(內容是自己取得後用特殊符號拼成字串) */
let scheme: any = jsb.reflection.callStaticMethod("AppController", "getIntent");
if (scheme && scheme !== "null") {
let Get_Arr: string[] = scheme.split("#");
for (let i: number = 0; i < Get_Arr.length; i++) {
let Get: string[] = Get_Arr[i].split("$");
HttpData[Get[0]] = Get[1];
}
let Query: string = HttpData["Query"];
if (Query) {
Get_Arr = Query.split("&");
for (let i: number = 0; i < Get_Arr.length; i++) {
let Get: string[] = Get_Arr[i].split("=");
this.URLscheme[Get[0]] = Get[1];
}
}
}
// cc.log("getIntent OK, " + getIntent);
break;
}
default: {
break;
}
}
}
}
//#endregion
//#region Custom Function
/**
* TTS_Play
* @param {string} msg msg
*/
public TTS_Play(msg: string): void {
switch (cc.sys.os) {
case cc.sys.OS_WINDOWS: {
// // 網頁端
break;
}
case cc.sys.OS_ANDROID: {
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "TTS_Play", "(Ljava/lang/String;)V", msg);
break;
}
case cc.sys.OS_IOS: {
jsb.reflection.callStaticMethod("AppController", "TTS_Play:", msg);
break;
}
default: {
break;
}
}
}
public HideNativeSplash(): void {
if (CC_JSB) {
if (cc.sys.os === cc.sys.OS_ANDROID) {
// 反射调用原生的隐藏方法
jsb.reflection.callStaticMethod(
"org/cocos2dx/javascript/AppActivity",
"hideSplash",
"()V"
);
}
}
}
public CCLOG(msg: String): void {
console.log(msg);
}
public DispatchCallback(msg: string): void {
let data: string[] = msg.split("=");
let data1: string[] = data[1] ? data[1].split("#") : [];
let scenename: string = cc.director.getScene().name;
// cc.director.on(cc.Director.EVENT_AFTER_SCENE_LAUNCH, callback, this);
// this.NativeCallback.DispatchCallback(data[0], data1);
}
public ClearAllCache(): void {
cc.sys.localStorage.clear();
cc.assetManager.bundles.clear();
cc.assetManager.cacheManager.clearCache();
cc.game.restart();
}
//#endregion
}
//#region Enum
export enum DeepLink {
ClearAllCache = "ClearAllCache",
}
//#endregion

View File

@ -1,6 +1,6 @@
{ {
"ver": "1.0.8", "ver": "1.0.8",
"uuid": "99fa9c05-c449-4a27-b629-42f77916556a", "uuid": "5558f747-ed9e-4040-9a01-3b89b99ac37a",
"isPlugin": false, "isPlugin": false,
"loadPluginInWeb": true, "loadPluginInWeb": true,
"loadPluginInNative": true, "loadPluginInNative": true,

View File

@ -1,41 +0,0 @@
const responsiveVoice = window["responsiveVoice"];
export default class Text_to_Speech {
//#region 外調參數
private voice: string = null;
//#endregion
//#region Custom
constructor() {
let self: this = this;
responsiveVoice.init();
var voicelist = responsiveVoice.getVoices();
this.setDefaultVoice(voicelist[14].name);
}
public setDefaultVoice(voice: string): void {
this.voice = voice;
responsiveVoice.setDefaultVoice(voice);
}
public speak(msg: string): void {
let self: this = this;
if (responsiveVoice.voiceSupport()) {
responsiveVoice.speak(msg, this.voice, {
onstart: function (Callback: any): void {
console.log(`onstart msg: ${msg}`);
},
onend: function (Callback: any): void {
console.log(`onstart msg: ${msg}`);
},
});
} else {
console.error(`!responsiveVoice.voiceSupport`);
}
}
//#endregion
}