|
|
|
@ -108,22 +108,17 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
static final int TYPE_GO_SAVESCREEN = 300;//导视通知跳转屏保
|
|
|
|
static final int TYPE_HINT_PASSWORD = 3;//隐藏密码输入框
|
|
|
|
static final int TYPE_START_SERVER = 4;//重新启动web服务
|
|
|
|
private volatile boolean isConnecting = false; // 添加此变量
|
|
|
|
static String HtmlUrl = "http://127.0.0.1:8080/index.html";//webServer服务地址 http://192.168.1.218:5173/
|
|
|
|
//static String HtmlUrl = "http://192.168.1.196:5500/index.html";
|
|
|
|
//static String HtmlUrl = "http://192.168.1.196:5173/";
|
|
|
|
int time = 0; |
|
|
|
private VideoView currentVideo; |
|
|
|
private VideoView proVideo; // 当前正在切换的目标视频
|
|
|
|
private SocketClient localSocketClient; |
|
|
|
private Map<String, Uri> videoMap = new HashMap<>(); |
|
|
|
private SoundPool soundPool; |
|
|
|
private int soundId; |
|
|
|
// 语音听写对象
|
|
|
|
private ASR mAsr; |
|
|
|
private Handler mainHandler; |
|
|
|
private boolean isrun = false; // 是否正在听写
|
|
|
|
private volatile Map<String, Uri> videoMap = new HashMap<>(); |
|
|
|
private final Handler mainHandler = new Handler(Looper.getMainLooper()); |
|
|
|
private String videoAfter = ""; |
|
|
|
private String language = "zh_cn"; |
|
|
|
private TTSUtil ttsUtil; |
|
|
|
private int countConect=0; |
|
|
|
|
|
|
|
Handler handler = new Handler() { |
|
|
|
@ -139,6 +134,7 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
Constant.isCoreService=true; |
|
|
|
LoggerUtil.e("WebViewActivity","WebView刷新"); |
|
|
|
}catch (Exception e){ |
|
|
|
LoggerUtil.e(TAG, "handleMessage refresh error", e); |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
@ -160,7 +156,9 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
case TYPE_HINT_PASSWORD: |
|
|
|
sendEmptyMessageDelayed(TYPE_HINT_PASSWORD,1000); |
|
|
|
if(time>=30){ |
|
|
|
binding.layoutPass.setVisibility(View.GONE); |
|
|
|
if (binding != null && binding.layoutPass != null) { |
|
|
|
binding.layoutPass.setVisibility(View.GONE); |
|
|
|
} |
|
|
|
removeMessages(TYPE_HINT_PASSWORD); |
|
|
|
} |
|
|
|
time++; |
|
|
|
@ -177,7 +175,6 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void initView() { |
|
|
|
mainHandler = new Handler(Looper.getMainLooper()); |
|
|
|
DeviceUtil.screenType(this); |
|
|
|
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); |
|
|
|
binding = DataBindingUtil.setContentView(this, R.layout.activity_webview); |
|
|
|
@ -195,78 +192,34 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
Uri videoUri2 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.speek); |
|
|
|
videoMap.put("hat_speek", videoUri2); |
|
|
|
|
|
|
|
//小红帽变身贵妇
|
|
|
|
// Uri videoUri3 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.hatto);
|
|
|
|
// videoMap.put("hat_to", videoUri3);
|
|
|
|
// //贵妇变身小红帽
|
|
|
|
// Uri videoUri4 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.hatback);
|
|
|
|
// videoMap.put("hat_back", videoUri4);
|
|
|
|
// //贵妇背景
|
|
|
|
// Uri videoUri5 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bg1);
|
|
|
|
// videoMap.put("hat_bg1", videoUri5);
|
|
|
|
// Uri videoUri6 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.hello1);
|
|
|
|
// videoMap.put("hat_hello1", videoUri6);
|
|
|
|
// Uri videoUri7 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.speek1);
|
|
|
|
// videoMap.put("hat_speek1", videoUri7);
|
|
|
|
|
|
|
|
soundPool = new SoundPool.Builder() |
|
|
|
.setMaxStreams(2) |
|
|
|
.build(); |
|
|
|
|
|
|
|
loadingbgVideo(); |
|
|
|
currentVideo = binding.bgVideo; |
|
|
|
proVideo = binding.bgVideo; |
|
|
|
loadingHelloVideo(); |
|
|
|
loadingSpeekVideo(); |
|
|
|
// loadingToVideo();
|
|
|
|
// loadingBackVideo();
|
|
|
|
// loadingbgVideo1();
|
|
|
|
// loadingHelloVideo1();
|
|
|
|
// loadingSpeekVideo1();
|
|
|
|
//设置密码
|
|
|
|
initPass(); |
|
|
|
if("sbc".equals(Constant.TTSFac)){ // 思必驰
|
|
|
|
connectLocalSocket(); |
|
|
|
}else{ //kdxf
|
|
|
|
initASR(); |
|
|
|
ttsUtil = new TTSUtil(); |
|
|
|
ttsUtil.initTts(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void playMusic(){ |
|
|
|
soundId = soundPool.load(this, R.raw.musice, 1); |
|
|
|
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { |
|
|
|
@Override |
|
|
|
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { |
|
|
|
if (status == 0) { |
|
|
|
soundPool.play(sampleId, 1.0f, 1.0f, 1, -1, 1.0f); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private void stopMusic(){ |
|
|
|
soundPool.stop(soundId); |
|
|
|
soundPool.unload(soundId); |
|
|
|
connectLocalSocket(); |
|
|
|
} |
|
|
|
|
|
|
|
private void loadingbgVideo(){ |
|
|
|
binding.bgVideo.setVideoURI(videoMap.get("hat_bg")); |
|
|
|
binding.bgVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { |
|
|
|
@Override |
|
|
|
public void onPrepared(MediaPlayer mp) { |
|
|
|
mp.setLooping(true); // 可选:设置循环播
|
|
|
|
binding.bgVideo.start(); |
|
|
|
} |
|
|
|
}); |
|
|
|
binding.bgVideo.setOnErrorListener(new MediaPlayer.OnErrorListener() { |
|
|
|
@Override |
|
|
|
public boolean onError(MediaPlayer mp, int what, int extra) { |
|
|
|
LoggerUtil.e("changeVideo: ","bgVideo 播放失败"); |
|
|
|
return true; // 返回true表示已处理,不会弹出系统默认提示
|
|
|
|
} |
|
|
|
}); |
|
|
|
if (binding != null && binding.bgVideo != null) { |
|
|
|
binding.bgVideo.setVideoURI(videoMap.get("hat_bg")); |
|
|
|
binding.bgVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { |
|
|
|
@Override |
|
|
|
public void onPrepared(MediaPlayer mp) { |
|
|
|
mp.setLooping(true); // 可选:设置循环播
|
|
|
|
binding.bgVideo.start(); |
|
|
|
} |
|
|
|
}); |
|
|
|
binding.bgVideo.setOnErrorListener(new MediaPlayer.OnErrorListener() { |
|
|
|
@Override |
|
|
|
public boolean onError(MediaPlayer mp, int what, int extra) { |
|
|
|
LoggerUtil.e("changeVideo: ","bgVideo 播放失败"); |
|
|
|
return true; // 返回true表示已处理,不会弹出系统默认提示
|
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
private void loadingHelloVideo(){ |
|
|
|
binding.helloVideo.setVideoURI(videoMap.get("hat_hello")); |
|
|
|
@ -302,180 +255,69 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
//小红帽变 to
|
|
|
|
// private void loadingToVideo(){
|
|
|
|
// binding.toVideo.setVideoURI(videoMap.get("hat_to"));
|
|
|
|
// binding.toVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
|
|
|
|
// @Override
|
|
|
|
// public void onPrepared(MediaPlayer mp) {
|
|
|
|
// binding.toVideo.start();
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// binding.toVideo.setOnErrorListener(new MediaPlayer.OnErrorListener() {
|
|
|
|
// @Override
|
|
|
|
// public boolean onError(MediaPlayer mp, int what, int extra) {
|
|
|
|
// LoggerUtil.e("changeVideo: ","toVideo 播放失败");
|
|
|
|
// return true; // 返回true表示已处理,不会弹出系统默认提示
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// binding.toVideo.setOnCompletionListener( mp -> changeVideo("bg"));
|
|
|
|
// }
|
|
|
|
//小红帽回 back
|
|
|
|
// private void loadingBackVideo(){
|
|
|
|
// binding.backVideo.setVideoURI(videoMap.get("hat_back"));
|
|
|
|
// binding.backVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
|
|
|
|
// @Override
|
|
|
|
// public void onPrepared(MediaPlayer mp) {
|
|
|
|
// binding.backVideo.start();
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// binding.backVideo.setOnErrorListener(new MediaPlayer.OnErrorListener() {
|
|
|
|
// @Override
|
|
|
|
// public boolean onError(MediaPlayer mp, int what, int extra) {
|
|
|
|
// LoggerUtil.e("changeVideo: ","backVideo 播放失败");
|
|
|
|
// return true; // 返回true表示已处理,不会弹出系统默认提示
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// binding.backVideo.setOnCompletionListener( mp -> {
|
|
|
|
// changeVideo("bg");
|
|
|
|
// stopMusic();
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
//彩蛋背景视频
|
|
|
|
// private void loadingbgVideo1(){
|
|
|
|
// binding.bgVideo1.setVideoURI(videoMap.get("hat_bg1"));
|
|
|
|
// binding.bgVideo1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
|
|
|
|
// @Override
|
|
|
|
// public void onPrepared(MediaPlayer mp) {
|
|
|
|
// mp.setLooping(true); // 可选:设置循环播
|
|
|
|
// binding.bgVideo1.start();
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// binding.bgVideo1.setOnErrorListener(new MediaPlayer.OnErrorListener() {
|
|
|
|
// @Override
|
|
|
|
// public boolean onError(MediaPlayer mp, int what, int extra) {
|
|
|
|
// LoggerUtil.e("changeVideo: ","backVideo 播放失败");
|
|
|
|
// return true; // 返回true表示已处理,不会弹出系统默认提示
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
//彩蛋hello视频
|
|
|
|
// private void loadingHelloVideo1(){
|
|
|
|
// binding.helloVideo1.setVideoURI(videoMap.get("hat_hello1"));
|
|
|
|
// binding.helloVideo1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
|
|
|
|
// @Override
|
|
|
|
// public void onPrepared(MediaPlayer mp) {
|
|
|
|
// mp.setLooping(true); // 可选:设置循环播
|
|
|
|
// binding.helloVideo1.start();
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// binding.helloVideo1.setOnErrorListener(new MediaPlayer.OnErrorListener() {
|
|
|
|
// @Override
|
|
|
|
// public boolean onError(MediaPlayer mp, int what, int extra) {
|
|
|
|
// LoggerUtil.e("changeVideo: ","helloVideo1 播放失败");
|
|
|
|
// return true; // 返回true表示已处理,不会弹出系统默认提示
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
//彩蛋Speek视频
|
|
|
|
// private void loadingSpeekVideo1(){
|
|
|
|
// binding.speekVideo1.setVideoURI(videoMap.get("hat_speek1"));
|
|
|
|
// binding.speekVideo1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
|
|
|
|
// @Override
|
|
|
|
// public void onPrepared(MediaPlayer mp) {
|
|
|
|
// mp.setLooping(true);
|
|
|
|
// binding.speekVideo1.start();
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// binding.speekVideo1.setOnErrorListener(new MediaPlayer.OnErrorListener() {
|
|
|
|
// @Override
|
|
|
|
// public boolean onError(MediaPlayer mp, int what, int extra) {
|
|
|
|
// LoggerUtil.e("changeVideo: ","speekVideo1 播放失败");
|
|
|
|
// return true; // 返回true表示已处理,不会弹出系统默认提示
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
|
|
|
//切换视频状态
|
|
|
|
private void changeVideoAfter(){ |
|
|
|
if("".equals(videoAfter)){ //新状态正常
|
|
|
|
changeVideo("back"); |
|
|
|
}else{ //新状态彩蛋
|
|
|
|
changeVideo("to"); |
|
|
|
playMusic(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//切换视频
|
|
|
|
public void changeVideo(String type){ |
|
|
|
LoggerUtil.e("changeVideo", type); |
|
|
|
if (type == null) { |
|
|
|
type = "bg"; |
|
|
|
} |
|
|
|
VideoView nextVideo; |
|
|
|
proVideo.clearAnimation(); |
|
|
|
currentVideo.clearAnimation(); |
|
|
|
if (proVideo != null) { |
|
|
|
proVideo.animate().cancel(); |
|
|
|
proVideo.clearAnimation(); |
|
|
|
} |
|
|
|
if (currentVideo != null) { |
|
|
|
currentVideo.animate().cancel(); |
|
|
|
currentVideo.clearAnimation(); |
|
|
|
} |
|
|
|
|
|
|
|
switch(type.trim()){ |
|
|
|
// case "back" :
|
|
|
|
// if(currentVideo == binding.backVideo){
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// nextVideo = binding.backVideo;
|
|
|
|
// break;
|
|
|
|
// case "to" :
|
|
|
|
// if(currentVideo == binding.toVideo){
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// nextVideo = binding.toVideo;
|
|
|
|
// break;
|
|
|
|
case "bg" : |
|
|
|
if(currentVideo == binding.bgVideo && "".equals(videoAfter)){ |
|
|
|
if(currentVideo == binding.bgVideo ){ |
|
|
|
return; |
|
|
|
} |
|
|
|
// if(currentVideo == binding.bgVideo1 && "1".equals(videoAfter)){
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
nextVideo = "".equals(videoAfter) ? binding.bgVideo : binding.bgVideo1; |
|
|
|
nextVideo = binding.bgVideo; |
|
|
|
break; |
|
|
|
case "answer" : |
|
|
|
if(currentVideo == binding.speekVideo && "".equals(videoAfter)){ |
|
|
|
if(currentVideo == binding.speekVideo){ |
|
|
|
return; |
|
|
|
} |
|
|
|
// if(currentVideo == binding.speekVideo1 && "1".equals(videoAfter)){
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
nextVideo = "".equals(videoAfter) ? binding.speekVideo : binding.speekVideo1; |
|
|
|
nextVideo = binding.speekVideo; |
|
|
|
break; |
|
|
|
case "hello" : |
|
|
|
if(currentVideo == binding.helloVideo && "".equals(videoAfter)){ |
|
|
|
if(currentVideo == binding.helloVideo ){ |
|
|
|
return; |
|
|
|
} |
|
|
|
// if(currentVideo == binding.helloVideo1 && "1".equals(videoAfter)){
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
nextVideo = "".equals(videoAfter) ? binding.helloVideo : binding.helloVideo1; |
|
|
|
nextVideo = binding.helloVideo; |
|
|
|
break; |
|
|
|
default: |
|
|
|
nextVideo = "".equals(videoAfter) ? binding.bgVideo : binding.bgVideo1; |
|
|
|
nextVideo = binding.bgVideo; |
|
|
|
} |
|
|
|
|
|
|
|
// 检查是否是重复切换
|
|
|
|
if (nextVideo == currentVideo) { |
|
|
|
if (nextVideo == null || nextVideo == currentVideo) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
nextVideo.animate().cancel(); |
|
|
|
nextVideo.clearAnimation(); |
|
|
|
proVideo = currentVideo; |
|
|
|
proVideo.animate() |
|
|
|
.alpha(0.5f) |
|
|
|
.setDuration(240) |
|
|
|
.setListener(new AnimatorListenerAdapter() { |
|
|
|
@Override |
|
|
|
public void onAnimationEnd(Animator animation) { |
|
|
|
super.onAnimationEnd(animation); |
|
|
|
proVideo.pause(); |
|
|
|
proVideo.setVisibility(View.GONE); |
|
|
|
proVideo.setAlpha(1.0f); |
|
|
|
} |
|
|
|
}) |
|
|
|
.start(); |
|
|
|
|
|
|
|
if (proVideo != null) { |
|
|
|
proVideo.animate() |
|
|
|
.alpha(0.5f) |
|
|
|
.setDuration(240) |
|
|
|
.setListener(new AnimatorListenerAdapter() { |
|
|
|
@Override |
|
|
|
public void onAnimationEnd(Animator animation) { |
|
|
|
super.onAnimationEnd(animation); |
|
|
|
if (proVideo != null) { |
|
|
|
proVideo.pause(); |
|
|
|
proVideo.setVisibility(View.GONE); |
|
|
|
proVideo.setAlpha(1.0f); |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
.start(); |
|
|
|
} |
|
|
|
nextVideo.setAlpha(0.5f); |
|
|
|
nextVideo.setVisibility(View.VISIBLE); |
|
|
|
|
|
|
|
@ -488,10 +330,12 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
@Override |
|
|
|
public void onAnimationEnd(Animator animation) { |
|
|
|
super.onAnimationEnd(animation); |
|
|
|
//currentVideo.seekTo(0);
|
|
|
|
currentVideo.start(); |
|
|
|
currentVideo.setAlpha(1.0f); |
|
|
|
LoggerUtil.e("切换视频:","动画播放完成"); |
|
|
|
if (currentVideo != null) { |
|
|
|
//currentVideo.seekTo(0);
|
|
|
|
currentVideo.start(); |
|
|
|
currentVideo.setAlpha(1.0f); |
|
|
|
LoggerUtil.e("切换视频:","动画播放完成"); |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
.start(); |
|
|
|
@ -629,6 +473,9 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
super.onDestroy(); |
|
|
|
destroyLocalSocketClient(); |
|
|
|
LoggerUtil.e(TAG, "onDestroy()"); |
|
|
|
if (handler != null) { |
|
|
|
handler.removeCallbacksAndMessages(null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void loadH5Url(boolean loadUrl) { |
|
|
|
@ -681,56 +528,30 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
@JavascriptInterface |
|
|
|
public void restartWebView() { |
|
|
|
LoggerUtil.e(TAG,"JS调用了Android的方法:restartWebView()"); |
|
|
|
handler.removeMessages(TYPE_REFURBISH_WEBVIEW); |
|
|
|
handler.sendEmptyMessageDelayed(TYPE_REFURBISH_WEBVIEW,3000); |
|
|
|
if (handler != null) { |
|
|
|
handler.removeMessages(TYPE_REFURBISH_WEBVIEW); |
|
|
|
handler.sendEmptyMessageDelayed(TYPE_REFURBISH_WEBVIEW,3000); |
|
|
|
} |
|
|
|
} |
|
|
|
// h5调用android:彩蛋
|
|
|
|
@JavascriptInterface |
|
|
|
public void changeVideoState(String txt) { |
|
|
|
LoggerUtil.e(TAG,"JS调用了Android的方法:changeVideoState()"); |
|
|
|
//videoAfter = txt;
|
|
|
|
// runOnUiThread(new Runnable() {
|
|
|
|
// @Override
|
|
|
|
// public void run() {
|
|
|
|
// changeVideoAfter();
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
} |
|
|
|
// h5调用android:开始识别语音
|
|
|
|
@JavascriptInterface |
|
|
|
public void startRecord() { |
|
|
|
LoggerUtil.e(TAG,"JS调用了Android的方法:startRecord()"); |
|
|
|
try{ |
|
|
|
if("sbc".equals(Constant.TTSFac)) { |
|
|
|
if(localSocketClient != null) { |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/tts/stop\"}")); |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/asr/start\"}")); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
if(isrun){ |
|
|
|
return; |
|
|
|
} |
|
|
|
ttsUtil.stopTTs(); |
|
|
|
if(mAsr == null){ |
|
|
|
LoggerUtil.d(TAG, "未初始化"); |
|
|
|
initASR(); |
|
|
|
} |
|
|
|
count++; |
|
|
|
int ret = mAsr.startListener(count+""); |
|
|
|
if(ret != 0){ |
|
|
|
LoggerUtil.d(TAG,"识别开启失败,错误码:"+ret+"\n"); |
|
|
|
}else{ |
|
|
|
isrun = true; |
|
|
|
} |
|
|
|
} |
|
|
|
if(localSocketClient != null) { |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/tts/stop\"}")); |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/asr/start\"}")); |
|
|
|
} |
|
|
|
}catch (Exception e){ |
|
|
|
LoggerUtil.e(TAG,e.getMessage()); |
|
|
|
} |
|
|
|
runOnUiThread(new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
runOnUiThread(() -> { |
|
|
|
changeVideo("bg"); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
// h5调用android:停止识别语音
|
|
|
|
@ -738,13 +559,9 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
public void stopRecord() { |
|
|
|
LoggerUtil.e(TAG,"JS调用了Android的方法:stopRecord()"); |
|
|
|
try{ |
|
|
|
if("sbc".equals(Constant.TTSFac)) { |
|
|
|
if(localSocketClient != null) { |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/asr/stop\"}")); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
stopAsr(); |
|
|
|
} |
|
|
|
if(localSocketClient != null) { |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/asr/stop\"}")); |
|
|
|
} |
|
|
|
}catch (Exception e){ |
|
|
|
LoggerUtil.e(TAG,e.getMessage()); |
|
|
|
} |
|
|
|
@ -755,13 +572,9 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
public void startTTS(String txt) { |
|
|
|
LoggerUtil.e(TAG,"JS调用了Android的方法:startTTS()"); |
|
|
|
try{ |
|
|
|
if("sbc".equals(Constant.TTSFac)) { |
|
|
|
if(localSocketClient != null) { |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\": \"/tts/start\",\"params\": {\"text\":\"", txt, "\", \"mode\":\"autoPlay\"}}")); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
ttsUtil.startTTs(txt); |
|
|
|
} |
|
|
|
if(localSocketClient != null) { |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\": \"/tts/start\",\"params\": {\"text\":\"", txt, "\", \"mode\":\"autoPlay\"}}")); |
|
|
|
} |
|
|
|
}catch (Exception e){ |
|
|
|
LoggerUtil.e(TAG,e.getMessage()); |
|
|
|
} |
|
|
|
@ -771,13 +584,9 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
public void stopTTS() { |
|
|
|
LoggerUtil.e(TAG,"JS调用了Android的方法:stopTTS()"); |
|
|
|
try{ |
|
|
|
if("sbc".equals(Constant.TTSFac)){ |
|
|
|
if(localSocketClient != null){ |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/tts/stop\"}")); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
ttsUtil.stopTTs(); |
|
|
|
} |
|
|
|
if(localSocketClient != null){ |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/tts/stop\"}")); |
|
|
|
} |
|
|
|
}catch (Exception e){ |
|
|
|
LoggerUtil.e(TAG,e.getMessage()); |
|
|
|
} |
|
|
|
@ -835,11 +644,11 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
handler.removeMessages(TYPE_START_SERVER); |
|
|
|
handler.sendEmptyMessageDelayed(TYPE_START_SERVER,1000); |
|
|
|
}catch (Exception e){ |
|
|
|
|
|
|
|
LoggerUtil.e(TAG, "Restart core service error", e); |
|
|
|
} |
|
|
|
break; |
|
|
|
case Constant.VOID_STOP: |
|
|
|
changeVideo("bg"); |
|
|
|
//changeVideo("bg");
|
|
|
|
break; |
|
|
|
case Constant.ACTION_UPDATE_PROG2://后台下发节目通知,重置导视倒计时。
|
|
|
|
LoggerUtil.e(TAG,"重启导视倒计时"); |
|
|
|
@ -847,66 +656,6 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//-------- 科大讯飞
|
|
|
|
private void initASR(){ |
|
|
|
if(mAsr == null){ |
|
|
|
mAsr = new ASR(); |
|
|
|
mAsr.registerCallbacks(mAsrCallbacks); |
|
|
|
mAsr.language(language);//语种,zh_cn:中文,en_us:英文。其他语种参见集成文档
|
|
|
|
mAsr.domain("iat");//应用领域,iat:日常用语。其他领域参见集成文档
|
|
|
|
mAsr.accent("mandarin");//方言,mandarin:普通话。方言仅当language为中文时才会生效。其他方言参见集成文档。
|
|
|
|
mAsr.vinfo(true);//返回子句结果对应的起始和结束的端点帧偏移值。
|
|
|
|
if("zh_cn".equals(language)){ |
|
|
|
mAsr.dwa("wpgs");//动态修正
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
AsrCallbacks mAsrCallbacks = new AsrCallbacks() { |
|
|
|
@Override |
|
|
|
public void onResult(ASR.ASRResult asrResult, Object o) { |
|
|
|
//以下信息需要开发者根据自身需求,如无必要,可不需要解析执行。
|
|
|
|
int status = asrResult.getStatus(); //结果数据状态,0:识别的第一块结果,1:识别中间结果,2:识别最后一块结果
|
|
|
|
String result = asrResult.getBestMatchText(); //识别结果
|
|
|
|
if(status == 0){ |
|
|
|
voiceContent(result); |
|
|
|
}else if(status == 2){ |
|
|
|
voiceContent(result); |
|
|
|
stopAsr(); |
|
|
|
}else{ |
|
|
|
voiceContent(result); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onError(ASR.ASRError asrError, Object o) { |
|
|
|
// 在非主线程中需要调用 WebView 方法时
|
|
|
|
runOnUiThread(new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
binding.web.evaluateJavascript("javascript: window.youAskOver('');", null); |
|
|
|
} |
|
|
|
}); |
|
|
|
stopAsr(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onBeginOfSpeech() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onEndOfSpeech() { |
|
|
|
LoggerUtil.e("onEndOfSpeech:", "window.youAskOver('');"); |
|
|
|
runOnUiThread(new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
binding.web.evaluateJavascript("javascript: window.youAskOver('');", null); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
}; |
|
|
|
/** |
|
|
|
* android调用js:传送识别语音内容 |
|
|
|
*/ |
|
|
|
@ -921,21 +670,13 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
private void stopAsr(){ |
|
|
|
if(isrun){ |
|
|
|
if(mAsr!=null){ |
|
|
|
mAsr.stopListener(false); |
|
|
|
} |
|
|
|
isrun = false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 思必驰----------------
|
|
|
|
private void initLocalSocketClient() { |
|
|
|
try { |
|
|
|
destroyLocalSocketClient(); |
|
|
|
URI localUri = new URI("ws://127.0.0.1:50002"); |
|
|
|
LoggerUtil.e("TTSSocketUri", localUri.toString()); |
|
|
|
destroyLocalSocketClient(); |
|
|
|
localSocketClient = new SocketClient(localUri, new Draft_6455()); |
|
|
|
setLocalSocketListener(); |
|
|
|
} catch (Throwable t) { |
|
|
|
@ -943,35 +684,46 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
} |
|
|
|
} |
|
|
|
public void connectLocalSocket() { |
|
|
|
// 状态拦截:正在连接 / 已连接 → 直接return
|
|
|
|
if (isConnecting || (localSocketClient != null && localSocketClient.isOpen())) { |
|
|
|
return; |
|
|
|
} |
|
|
|
isConnecting = true; |
|
|
|
try { |
|
|
|
//客户端不存在时 创建客户端设置监听事件
|
|
|
|
if (localSocketClient != null && |
|
|
|
localSocketClient.getReadyState() == ReadyState.OPEN) { |
|
|
|
LoggerUtil.e("connectLocalSocket", "socket客户端连接状态:open"); |
|
|
|
countConect = 0; |
|
|
|
return; // 连接正常,直接返回
|
|
|
|
// 无实例 → 初始化
|
|
|
|
if (localSocketClient == null) { |
|
|
|
initLocalSocketClient(); |
|
|
|
} |
|
|
|
if (localSocketClient == null) initLocalSocketClient(); |
|
|
|
localSocketClient.connect(); |
|
|
|
countConect++; |
|
|
|
if(countConect>=100){ |
|
|
|
LoggerUtil.e("Socket: ", "socket未连接,设备重启"); |
|
|
|
// 最终空判断
|
|
|
|
if (localSocketClient == null) { |
|
|
|
isConnecting = false; |
|
|
|
scheduleReconnect(); |
|
|
|
return; |
|
|
|
} |
|
|
|
if(countConect>=102){ |
|
|
|
SignWayUtil.reboot(); |
|
|
|
// 执行连接
|
|
|
|
if (!localSocketClient.isOpen()) { |
|
|
|
localSocketClient.connect(); |
|
|
|
} |
|
|
|
} catch (Throwable t) { |
|
|
|
LoggerUtil.e("connectLocalSocket: Throwable", StringUtil.getThrowableStr(t)); |
|
|
|
destroyLocalSocketClient(); |
|
|
|
} catch (Exception e) { |
|
|
|
LoggerUtil.e("connectLocalSocket", "连接异常:" + e.getMessage()); |
|
|
|
isConnecting = false; |
|
|
|
scheduleReconnect(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//语音交互
|
|
|
|
private void setLocalSocketListener() { |
|
|
|
if (localSocketClient == null) return; |
|
|
|
localSocketClient.setOnOpenListener((handshakeData) -> { |
|
|
|
LoggerUtil.e("connectLocalSocket", "socket连接成功"); |
|
|
|
if(localSocketClient != null){ |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/asr/stop\"}")); |
|
|
|
countConect = 0; |
|
|
|
isConnecting = false; |
|
|
|
try { |
|
|
|
if(localSocketClient != null){ |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/asr/stop\"}")); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
LoggerUtil.e("connectLocalSocket发送: ", e.getMessage()); |
|
|
|
} |
|
|
|
}); |
|
|
|
localSocketClient.setOnMessageListener((conn, message) -> { |
|
|
|
@ -1011,21 +763,6 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
} |
|
|
|
}else if("hardware.status".equals(method)){ // 网络变化
|
|
|
|
LoggerUtil.e("ttsSocket: hardware.status", content.getNetwork().trim()); |
|
|
|
// if("lan".equals(content.getNetwork().trim())) { // 网络断开
|
|
|
|
// runOnUiThread(new Runnable() {
|
|
|
|
// @Override
|
|
|
|
// public void run() {
|
|
|
|
// binding.web.loadUrl(StringUtil.strSplice("javascript:window.netWorkNot('0');"));
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }else{
|
|
|
|
// runOnUiThread(new Runnable() {
|
|
|
|
// @Override
|
|
|
|
// public void run() {
|
|
|
|
// binding.web.loadUrl(StringUtil.strSplice("javascript:window.netWorkNot('1');"));
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
} |
|
|
|
} catch (Throwable t) { |
|
|
|
LoggerUtil.e("setOnMessageListener", StringUtil.getThrowableStr(t)); |
|
|
|
@ -1034,40 +771,48 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
|
|
|
|
localSocketClient.setOnCloseListener((code, reason, remote) -> { |
|
|
|
LoggerUtil.e("LocSocCliManager", "onClose:"+countConect); |
|
|
|
isConnecting = false; |
|
|
|
scheduleReconnect(); |
|
|
|
|
|
|
|
}); |
|
|
|
localSocketClient.setOnErrorListener((ex) -> { |
|
|
|
isConnecting = false; |
|
|
|
LoggerUtil.e("LocSocCliManager", "Error:"+countConect); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
//
|
|
|
|
private void scheduleReconnect() { |
|
|
|
mainHandler.removeCallbacksAndMessages(null); |
|
|
|
countConect++; |
|
|
|
if (countConect >= 100) { |
|
|
|
SignWayUtil.reboot(); |
|
|
|
return; // 避免继续重连
|
|
|
|
} |
|
|
|
destroyLocalSocketClient(); |
|
|
|
mainHandler.removeCallbacks(delayedTask); |
|
|
|
mainHandler.postDelayed(() -> { |
|
|
|
new Thread(() -> connectLocalSocket()).start(); |
|
|
|
}, 6000); // 3秒后重连
|
|
|
|
|
|
|
|
mainHandler.postDelayed(this::connectLocalSocket, 6000); |
|
|
|
} |
|
|
|
private Runnable delayedTask = new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
connectLocalSocket(); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
//销毁当前的客户端
|
|
|
|
public void destroyLocalSocketClient() { |
|
|
|
if (localSocketClient == null) return; |
|
|
|
|
|
|
|
try { |
|
|
|
if (localSocketClient != null) { |
|
|
|
// 避免在已关闭或未打开的状态下再次关闭
|
|
|
|
if (localSocketClient.isOpen()) { |
|
|
|
localSocketClient.close(); // 主动发送关闭帧
|
|
|
|
} |
|
|
|
localSocketClient = null; |
|
|
|
// 清空监听,避免内存泄漏/回调错乱
|
|
|
|
localSocketClient.setOnOpenListener(null); |
|
|
|
localSocketClient.setOnMessageListener(null); |
|
|
|
localSocketClient.setOnCloseListener(null); |
|
|
|
localSocketClient.setOnErrorListener(null); |
|
|
|
|
|
|
|
if (localSocketClient.isOpen()) { |
|
|
|
localSocketClient.close(); |
|
|
|
} |
|
|
|
} catch (Throwable t) { |
|
|
|
// 这里必须捕获所有异常,防止它传播出去导致进程崩溃
|
|
|
|
LoggerUtil.e("destroyLocalSocketClient", "清理时发生异常"); |
|
|
|
} catch (Exception e) { |
|
|
|
LoggerUtil.e("destroy", "关闭异常"); |
|
|
|
} finally { |
|
|
|
localSocketClient = null; |
|
|
|
isConnecting = false; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|