|
|
|
@ -12,6 +12,7 @@ import android.os.Build; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.os.Debug; |
|
|
|
import android.os.Handler; |
|
|
|
import android.os.Looper; |
|
|
|
import android.os.Message; |
|
|
|
import android.util.DisplayMetrics; |
|
|
|
import android.util.Log; |
|
|
|
@ -111,6 +112,7 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
private Map<String, Uri> videoMap = new HashMap<>(); |
|
|
|
// 语音听写对象
|
|
|
|
private ASR mAsr; |
|
|
|
private Handler mainHandler; |
|
|
|
private boolean isrun = false; // 是否正在听写
|
|
|
|
private String videoAfter = ""; |
|
|
|
private String language = "zh_cn"; |
|
|
|
@ -167,6 +169,7 @@ 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); |
|
|
|
@ -663,8 +666,10 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
LoggerUtil.e(TAG,"JS调用了Android的方法:startRecord()"); |
|
|
|
try{ |
|
|
|
if("sbc".equals(Constant.TTSFac)) { |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/tts/stop\"}")); |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/asr/start\"}")); |
|
|
|
if(localSocketClient != null) { |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/tts/stop\"}")); |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/asr/start\"}")); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
if(isrun){ |
|
|
|
return; |
|
|
|
@ -698,7 +703,9 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
LoggerUtil.e(TAG,"JS调用了Android的方法:stopRecord()"); |
|
|
|
try{ |
|
|
|
if("sbc".equals(Constant.TTSFac)) { |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/asr/stop\"}")); |
|
|
|
if(localSocketClient != null) { |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/asr/stop\"}")); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
stopAsr(); |
|
|
|
} |
|
|
|
@ -713,7 +720,9 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
LoggerUtil.e(TAG,"JS调用了Android的方法:startTTS()"); |
|
|
|
try{ |
|
|
|
if("sbc".equals(Constant.TTSFac)) { |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\": \"/tts/start\",\"params\": {\"text\":\"", txt, "\", \"mode\":\"autoPlay\"}}")); |
|
|
|
if(localSocketClient != null) { |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\": \"/tts/start\",\"params\": {\"text\":\"", txt, "\", \"mode\":\"autoPlay\"}}")); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
ttsUtil.startTTs(txt); |
|
|
|
} |
|
|
|
@ -727,7 +736,9 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
LoggerUtil.e(TAG,"JS调用了Android的方法:stopTTS()"); |
|
|
|
try{ |
|
|
|
if("sbc".equals(Constant.TTSFac)){ |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/tts/stop\"}")); |
|
|
|
if(localSocketClient != null){ |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/tts/stop\"}")); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
ttsUtil.stopTTs(); |
|
|
|
} |
|
|
|
@ -886,6 +897,7 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
// 思必驰----------------
|
|
|
|
private void initLocalSocketClient() { |
|
|
|
try { |
|
|
|
destroyLocalSocketClient(); |
|
|
|
URI localUri = new URI("ws://127.0.0.1:50002"); |
|
|
|
LoggerUtil.e("TTSSocketUri", localUri.toString()); |
|
|
|
localSocketClient = new SocketClient(localUri, new Draft_6455()); |
|
|
|
@ -898,21 +910,11 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
try { |
|
|
|
//客户端不存在时 创建客户端设置监听事件
|
|
|
|
if (localSocketClient == null) initLocalSocketClient(); |
|
|
|
//获取客户端当前的连接状态
|
|
|
|
ReadyState state = localSocketClient.getReadyState(); |
|
|
|
LoggerUtil.e("connectLocalSocket", "socket客户端连接状态:"+ GsonUtil.getGson().toJson(state)); |
|
|
|
boolean open = localSocketClient.isOpen(); |
|
|
|
LoggerUtil.e("connectLocalSocket", "socket客户端连接状态:open="+open); |
|
|
|
if (open) return; |
|
|
|
//未连接状态时 连接服务器
|
|
|
|
if (state.equals(ReadyState.NOT_YET_CONNECTED)) { |
|
|
|
LoggerUtil.e("connectLocalSocket", "socket客户端重新链接connect"); |
|
|
|
localSocketClient.connect(); |
|
|
|
} else if (state.equals(ReadyState.CLOSING) || state.equals(ReadyState.CLOSED)) { |
|
|
|
LoggerUtil.e("connectLocalSocket", "socket客户端重新链接reconnect"); |
|
|
|
//正在关闭或者关闭状态时 重新连接服务器
|
|
|
|
localSocketClient.reconnect(); |
|
|
|
} |
|
|
|
LoggerUtil.e("connectLocalSocket", "socket客户端连接connect"); |
|
|
|
localSocketClient.connect(); |
|
|
|
} catch (Throwable t) { |
|
|
|
LoggerUtil.e("connectLocalSocket: Throwable", StringUtil.getThrowableStr(t)); |
|
|
|
destroyLocalSocketClient(); |
|
|
|
@ -922,8 +924,10 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
//语音交互
|
|
|
|
private void setLocalSocketListener() { |
|
|
|
localSocketClient.setOnOpenListener((handshakeData) -> { |
|
|
|
LoggerUtil.e("connectLocalSocket", "TTS客户端连接成功"); |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/asr/stop\"}")); |
|
|
|
LoggerUtil.e("connectLocalSocket", "socket连接成功"); |
|
|
|
if(localSocketClient != null){ |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/asr/stop\"}")); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
localSocketClient.setOnMessageListener((conn, message) -> { |
|
|
|
@ -945,8 +949,10 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
runOnUiThread(new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/asr/stop\"}")); |
|
|
|
binding.web.loadUrl(StringUtil.strSplice("javascript:window.youAskOver('", content.getText(), "');")); |
|
|
|
if(localSocketClient != null){ |
|
|
|
localSocketClient.send(StringUtil.strSplice("{\"method\":\"/asr/stop\"}")); |
|
|
|
binding.web.loadUrl(StringUtil.strSplice("javascript:window.youAskOver('", content.getText(), "');")); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}else if("tts.result".equals(method)){ |
|
|
|
@ -984,13 +990,24 @@ public class WebViewActivity extends BaseActivity { |
|
|
|
|
|
|
|
localSocketClient.setOnCloseListener((code, reason, remote) -> { |
|
|
|
LoggerUtil.e("LocSocCliManager", "onClose:code="+code); |
|
|
|
connectLocalSocket(); |
|
|
|
scheduleReconnect(); |
|
|
|
}); |
|
|
|
localSocketClient.setOnErrorListener((ex) -> { |
|
|
|
LoggerUtil.e("LocSocCliManager", ex.getMessage()); |
|
|
|
scheduleReconnect(); |
|
|
|
}); |
|
|
|
// localSocketClient.setOnErrorListener((ex) -> {
|
|
|
|
// LoggerUtil.e("LocSocCliManager", ex.getMessage());
|
|
|
|
// //connectLocalSocket();
|
|
|
|
// });
|
|
|
|
} |
|
|
|
private void scheduleReconnect() { |
|
|
|
destroyLocalSocketClient(); |
|
|
|
mainHandler.removeCallbacks(delayedTask); |
|
|
|
mainHandler.postDelayed(delayedTask, 2000); // 2秒后重连
|
|
|
|
} |
|
|
|
private Runnable delayedTask = new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
connectLocalSocket(); |
|
|
|
} |
|
|
|
}; |
|
|
|
//销毁当前的客户端
|
|
|
|
public void destroyLocalSocketClient() { |
|
|
|
if (localSocketClient == null) return; |
|
|
|
|