1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
using Assets.SDK;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
using UnityEngine;
using XUtliPoolLib;
class XIFlyMgr : MonoBehaviour, IXIFlyMgr
{
string current;
private JoyYouSDK _interface;
public static string PARAMS = "params";
public const string LANGUAGE = "language"; // 语言
public const string en_us = "en_us"; // 英文
public const string zh_cn = "zh_cn"; // 中文
public const string mandarin = "mandarin "; // 普通话
public const string ENGINE_TYPE = "engine_type"; // 引擎类型
public const string TYPE_LOCAL = "local"; // 本地方式
public const string TYPE_CLOUD = "cloud"; // 服务器方式
public const string ACCENT = "accent"; // 方言
public const string AUDIO_FORMAT = "audio_format"; // 流保存的格式,有pcm,wav两种
public const string ASR_AUDIO_PATH = "asr_audio_path"; // 保存的路径
public const string SPEECH_TIME_OUT = "speech_timeout"; //超时设置
public const string VAD_BOS = "vad_bos";
public const string VAD_EOS = "vad_eos"; //后端超时
string filepath = "";
string cachePath = "";
bool inited = false;
[SerializeField]
string android_appid = "";
[SerializeField]
string ios_appid = "";
public bool Deprecated
{
get;
set;
}
#if UNITY_IPHONE
[DllImport("__Internal")]
private static extern bool saveToGallery( string path );
#endif
void Start()
{
#if UNITY_ANDROID && !UNITY_EDITOR
InitIFly(android_appid);
#elif UNITY_IOS && !UNITY_EDITOR
InitIFly(ios_appid);
#endif
SetCallback(TextRecCallback);
_interface = new JoyYouSDK();
#if !UNITY_EDITOR
// 先初始化为网络
SetParameter(ENGINE_TYPE, TYPE_CLOUD);
SetParameter(AUDIO_FORMAT, "pcm");
//SetParameter(SPEECH_TIME_OUT, "-1");
SetParameter(VAD_BOS, "8000");
SetParameter(VAD_EOS, "5000");
string audioPath = GetAudioCachePath();
if (!string.IsNullOrEmpty(audioPath))
{
filepath = audioPath + "/record.pcm";
Debug.Log("PCM filepath: " + filepath);
SetParameter(ASR_AUDIO_PATH, filepath);
inited = true;
}
//lame.Lame_InitEncoder(1, 16000, 16000, 0, 0, 0, 3.0f);
#else
inited = true;
#endif
}
void InitIFly(string appid)
{
Microphone.IsRecording(null);
//Debug.Log("Voice manager Init, appid: " + appid);
//Voice.Init(appid);
}
public bool IsIFlyListening()
{
return isListening;
}
public bool IsInited()
{
return inited;
}
public bool IsRecordFileExist()
{
return File.Exists(filepath);
}
public bool isListening
{
get
{
return false;// Voice.isListening();
}
}
public MonoBehaviour GetMonoBehavior()
{
return this;
}
public void SetParameter(string var1, string var2)
{
//SalfCall(() =>
//{
// Voice.SetParameter(var1, var2);
//});
}
void SalfCall(System.Action action)
{
try
{
action();
}
catch (System.Exception ex)
{
Debug.LogError(ex);
}
}
public int StartRecord()
{
Debug.Log("SpeechRecognizer:Start");
int code = 0;
//SalfCall(() =>
//{
// code = Voice.Start();
//});
Debug.Log(string.Format("SpeechRecognizer:Start({0})", code));
return code;
}
public void StopRecord()
{
Debug.Log("SpeechRecognizer:Stop");
//SalfCall(() =>
//{
// Voice.Stop();
//});
}
public void Cancel()
{
Debug.Log("SpeechRecognizer:Cancel");
//SalfCall(() =>
//{
// Voice.Cancel();
//});
}
void InitEnd(string code)
{
Debug.Log(string.Format("SpeechRecognizer:InitEnd:" + code));
}
void onBeginOfSpeech(string text)
{
Debug.Log("onBeginOfSpeech");
}
void onError(string text)
{
Debug.Log(string.Format("onError:{0}", text));
}
void onEndOfSpeech(string text)
{
Debug.Log("onEndOfSpeech");
}
void onVolumeChanged(string volume)
{
//Debug.Log("onVolumeChanged:" + volume);
if (voicecallback != null)
voicecallback(volume);
}
System.Action<string> callback = null;
System.Action<string> voicecallback = null;
public void SetCallback(System.Action<string> action)
{
callback = action;
}
public void SetVoiceCallback(System.Action<string> action)
{
voicecallback = action;
}
void onResult(string text)
{
if (callback != null)
{
callback(text);
}
}
string GetAudioCachePath()
{
if (string.IsNullOrEmpty(cachePath))
{
#if UNITY_IOS || UNITY_EDITOR
cachePath = Application.temporaryCachePath;
#else
cachePath = Application.persistentDataPath;
#endif
if (string.IsNullOrEmpty(cachePath))
return "";
if (!Directory.Exists(cachePath))
Directory.CreateDirectory(cachePath);
}
return cachePath;
}
string GetString(string path)
{
if (string.IsNullOrEmpty(path))
return path;
return path.Replace('\\', '/');
}
public string StartTransMp3(string destFileName)
{
//string dstFilePath = filepath.Substring(0, filepath.LastIndexOf('.')) + destFileName + ".mp3";
//Debug.Log ("srcFIlePath: " + filepath + ", dst file path:" + dstFilePath);
//lame.Lame_EncodeFile(filepath, dstFilePath);
//return dstFilePath;
return "";
}
public AudioClip GetAudioClip(string filepath)
{
#if UNITY_ANDROID || UNITY_IOS
return null;
#else
//return XBehaviorTree.GetAudioClip(filepath);
return null;
#endif
//StartCoroutine(LoadAudioClip(filepath, callback1, callback2));
}
void TextRecCallback(string res)
{
}
public bool ScreenShotQQShare(string filepath, string type)
{
if (!File.Exists(filepath))
return false;
// Construct content json string
Dictionary<string, string> jsonData = new Dictionary<string, string>();
//JsonData jsonData = new JsonData();
//jsonData["scene"] = "Session";
//jsonData["scene"] = "QZone";
jsonData["scene"] = type;
jsonData["filePath"] = filepath;
// jsonData["imageUrl"] = "http://f.hiphotos.baidu.com/baike/pic/item/5fdf8db1cb134954643deefc564e9258d1094a02.jpg";
string paramStr = MiniJSON.Json.Serialize(jsonData);
Debug.Log("SharePhotoWithQQ paramStr = " + paramStr);
((IHuanlePlatform)_interface).SendGameExtData("share_send_to_with_photo_qq", paramStr);
return true;
}
public bool ScreenShotWeChatShare(string filepath, string type)
{
if (!File.Exists(filepath))
return false;
// Construct content json string
Dictionary<string, string> jsonData = new Dictionary<string, string>();
jsonData["scene"] = type;
jsonData["filePath"] = filepath;
jsonData["mediaTagName"] = "MSG_INVITE";
jsonData["messageExt"] = "SharePhotoWithWeixin";
jsonData["messageAction"] = "WECHAT_SNS_JUMP_APP";
string paramStr = MiniJSON.Json.Serialize(jsonData);
Debug.Log("SharePhotoWithWeixin paramStr = " + paramStr);
((IHuanlePlatform)_interface).SendGameExtData("share_send_to_with_photo_wx", paramStr);
return true;
}
public bool ScreenShotSave(string filepath)
{
#if UNITY_IPHONE
return saveToGallery(filepath);
#endif
return true;
}
public bool RefreshAndroidPhotoView(string androidpath)
{
#if UNITY_ANDROID
AndroidJavaClass obj = new AndroidJavaClass("com.ryanwebb.androidscreenshot.MainActivity");
obj.CallStatic<bool>("scanMedia", androidpath);
#endif
return true;
}
public bool OnOpenWebView()
{
Debug.Log("OnOpenWebView");
#if UNITY_IOS || UNITY_ANDROID
UniWeb web = GetComponent<UniWeb>();
if (web != null)
{
web.OpenWebView();
}
#endif
return true;
}
public void OnCloseWebView()
{
#if UNITY_IOS || UNITY_ANDROID
UniWeb web = GetComponent<UniWeb>();
if (web != null)
{
web.CloseWebView(null);
}
#endif
}
public void OnInitWebViewInfo(int platform, string openid, string serverid, string roleid, string nickname)
{
#if UNITY_IOS || UNITY_ANDROID
UniWeb web = GetComponent<UniWeb>();
if (web != null)
web.InitWebInfo(platform, openid, serverid, roleid, nickname);
#endif
}
public bool ShareWechatLink(string desc, string logopath, string url, bool issession)
{
// Construct content json string
Dictionary<string, object> jsonData = new Dictionary<string, object>();
if (issession)
jsonData["scene"] = "Session";
else
jsonData["scene"] = "Timeline";
jsonData["title"] = desc;
jsonData["desc"] = desc;
jsonData["url"] = url;
jsonData["mediaTagName"] = "MSG_INVITE";
jsonData["filePath"] = logopath;
jsonData["messageExt"] = "ShareUrlWithWeixin";
string paramStr = MiniJSON.Json.Serialize(jsonData);
Debug.Log("ShareUrlWithWeixin paramStr = " + paramStr);
((IHuanlePlatform)_interface).SendGameExtData("share_send_to_with_url_wx", paramStr);
return true;
}
public bool ShareWechatLinkWithMediaTag(string desc, string logopath, string url, bool issession, string media)
{
Dictionary<string, object> jsonData = new Dictionary<string, object>();
if (issession)
jsonData["scene"] = "Session";
else
jsonData["scene"] = "Timeline";
jsonData["title"] = desc;
jsonData["desc"] = desc;
jsonData["url"] = url;
jsonData["mediaTagName"] = media;
jsonData["filePath"] = logopath;
jsonData["messageExt"] = "ShareUrlWithWeixin";
string paramStr = MiniJSON.Json.Serialize(jsonData);
Debug.Log("ShareUrlWithWeixin paramStr = " + paramStr);
((IHuanlePlatform)_interface).SendGameExtData("share_send_to_with_url_wx", paramStr);
return true;
}
public bool ShareQZoneLink(string title, string summary, string url, string logopath, bool issession)
{
// Construct content json string
Dictionary<string, object> jsonData = new Dictionary<string, object>();
if (issession)
jsonData["scene"] = "Session";
else
jsonData["scene"] = "QZone";
jsonData["title"] = title;
jsonData["summary"] = summary;
jsonData["targetUrl"] = url;
jsonData["imageUrl"] = logopath;
// jsonData["imageUrl"] = "http://f.hiphotos.baidu.com/baike/pic/item/5fdf8db1cb134954643deefc564e9258d1094a02.jpg";
string paramStr = MiniJSON.Json.Serialize(jsonData);
Debug.Log("ShareWithQQClient paramStr = " + paramStr);
((IHuanlePlatform)_interface).SendGameExtData("share_send_to_struct_qq", paramStr);
return true;
}
public void OnEvalJsScript(string script)
{
#if UNITY_IOS || UNITY_ANDROID
UniWeb web = GetComponent<UniWeb>();
if (web != null)
{
web.EvalJsScript(script);
}
#endif
}
public void OnScreenLock(bool islock)
{
#if UNITY_IOS || UNITY_ANDROID
UniWeb web = GetComponent<UniWeb>();
if (web != null)
{
Debug.Log("Will eval screen lock");
if (islock)
web.EvalJsScript("DNScreenLock()");
else
web.EvalJsScript("DNScreenUnlock()");
}
#endif
}
public void RefershWebViewShow(bool show)
{
#if UNITY_IOS || UNITY_ANDROID
UniWeb web = GetComponent<UniWeb>();
if (web != null)
{
Debug.Log("Will eval screen lock");
web.OnShowWebView(show);
}
#endif
}
}
|