10 changed files with 1124 additions and 104 deletions
@ -0,0 +1,800 @@ |
|||
package qianmu.container.activity.H5; |
|||
|
|||
import android.animation.Animator; |
|||
import android.animation.AnimatorListenerAdapter; |
|||
import android.annotation.SuppressLint; |
|||
import android.app.ActivityManager; |
|||
import android.content.Context; |
|||
import android.content.Intent; |
|||
import android.graphics.Bitmap; |
|||
import android.media.MediaMetadataRetriever; |
|||
import android.media.MediaPlayer; |
|||
import android.media.SoundPool; |
|||
import android.net.Uri; |
|||
import android.os.Build; |
|||
import android.os.Bundle; |
|||
import android.os.Debug; |
|||
import android.os.Handler; |
|||
import android.os.Message; |
|||
import android.util.DisplayMetrics; |
|||
import android.util.Log; |
|||
import android.view.KeyEvent; |
|||
import android.view.View; |
|||
import android.view.ViewGroup; |
|||
import android.view.WindowManager; |
|||
import android.view.animation.AccelerateDecelerateInterpolator; |
|||
import android.view.animation.AlphaAnimation; |
|||
import android.view.animation.Animation; |
|||
import android.webkit.ConsoleMessage; |
|||
import android.webkit.JavascriptInterface; |
|||
import android.webkit.WebChromeClient; |
|||
import android.webkit.WebSettings; |
|||
import android.webkit.WebView; |
|||
import android.webkit.WebViewClient; |
|||
import android.widget.FrameLayout; |
|||
import android.widget.VideoView; |
|||
|
|||
import androidx.annotation.NonNull; |
|||
import androidx.core.view.ViewCompat; |
|||
import androidx.databinding.DataBindingUtil; |
|||
import androidx.recyclerview.widget.GridLayoutManager; |
|||
|
|||
import com.aispeech.AIError; |
|||
import com.aispeech.AIResult; |
|||
import com.aispeech.common.AIConstant; |
|||
import com.aispeech.common.JSONResultParser; |
|||
import com.aispeech.export.config.AICloudASRConfig; |
|||
import com.aispeech.export.engines2.AICloudASREngine; |
|||
import com.aispeech.export.intent.AICloudASRIntent; |
|||
import com.aispeech.export.listeners.AIASRListener; |
|||
import com.alibaba.android.arouter.facade.annotation.Route; |
|||
import com.alibaba.android.arouter.launcher.ARouter; |
|||
|
|||
import org.greenrobot.eventbus.EventBus; |
|||
import org.json.JSONArray; |
|||
import org.json.JSONException; |
|||
import org.json.JSONObject; |
|||
|
|||
import java.text.SimpleDateFormat; |
|||
import java.util.ArrayList; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Locale; |
|||
import java.util.Map; |
|||
|
|||
import qianmu.container.R; |
|||
import qianmu.container.activity.BaseActivity; |
|||
import qianmu.container.adapter.KeyAdapter; |
|||
import qianmu.container.app.Constant; |
|||
import qianmu.container.app.MyApplication; |
|||
import qianmu.container.data.AppData; |
|||
import qianmu.container.data.DeviceData; |
|||
import qianmu.container.data.ScreenSaverData; |
|||
import qianmu.container.databinding.ActivityWebviewBinding; |
|||
import qianmu.container.entity.MessageEvent; |
|||
import qianmu.container.socket.SocketClient; |
|||
import qianmu.container.util.CalendarUtils; |
|||
import qianmu.container.util.DeviceUtil; |
|||
import qianmu.container.util.FileUtil; |
|||
import qianmu.container.util.KeyboardUtil; |
|||
import qianmu.container.util.LoggerUtil; |
|||
import qianmu.container.util.StringUtil; |
|||
import qianmu.container.util.TTSUtil; |
|||
import qianmu.container.util.ToastUtils; |
|||
|
|||
import static qianmu.container.service.ContainerService.initFirstTime; |
|||
|
|||
@Route(path = Constant.ROUTE_WEB_VIEW) |
|||
public class WebViewActivity extends BaseActivity { |
|||
|
|||
String TAG="WebViewActivity"; |
|||
ActivityWebviewBinding binding; |
|||
static final int TYPE_REFURBISH_WEBVIEW = 100;//刷新webview
|
|||
static final int TYPE_GO_SAVESCREEN = 300;//导视通知跳转屏保
|
|||
static final int TYPE_HINT_PASSWORD = 3;//隐藏密码输入框
|
|||
static final int TYPE_START_SERVER = 4;//重新启动web服务
|
|||
static String HtmlUrl = "http://127.0.0.1:8080/index.html";//webServer服务地址 http://192.168.1.218:5173/
|
|||
//static String HtmlUrl = "http://192.168.0.109:5173/";
|
|||
int time = 0; |
|||
private VideoView currentVideo; //当前
|
|||
private VideoView proVideo; //上一个视频
|
|||
private boolean isChatMan=false; //是否是数字人项目
|
|||
private SoundPool soundPool; |
|||
private int soundId; |
|||
// 语音听写对象
|
|||
private AICloudASREngine mEngine; |
|||
private AICloudASRIntent aiCloudASRIntent; |
|||
private TTSUtil ttsUtil; |
|||
private String oldString=""; |
|||
private Map<String, VideoView> videoMap = new HashMap<>(); |
|||
private String videoAfter=""; |
|||
|
|||
Handler handler = new Handler() { |
|||
@SuppressLint("HandlerLeak") |
|||
@Override |
|||
public void handleMessage(@NonNull Message msg) { |
|||
super.handleMessage(msg); |
|||
switch (msg.what){ |
|||
case TYPE_REFURBISH_WEBVIEW://刷新webview
|
|||
try { |
|||
binding.web.clearCache(true); |
|||
binding.web.loadUrl(HtmlUrl); |
|||
Constant.isCoreService=true; |
|||
}catch (Exception e){ |
|||
} |
|||
break; |
|||
|
|||
case TYPE_GO_SAVESCREEN://导视通知跳转屏保
|
|||
try { |
|||
initFirstTime(); |
|||
if (!ScreenSaverData.isProgramPlay()) return; |
|||
|
|||
if (AppData.getFrontApp().equals(DeviceUtil.getPackageName())) { //信发处于系统前端时 显示屏保页面
|
|||
EventBus.getDefault().post(new MessageEvent(Constant.ACTION_SHOW_SPLASH)); |
|||
} else { //重新启动信发软件并打开屏保页面
|
|||
ARouter.getInstance().build(Constant.ROUTE_SCREEN_SAVER).navigation(); |
|||
} |
|||
} catch (Throwable t) { |
|||
LoggerUtil.e("WebViewActivity()", "导视通知跳转报错:"+StringUtil.getThrowableStr(t)); |
|||
} |
|||
break; |
|||
|
|||
case TYPE_HINT_PASSWORD: |
|||
sendEmptyMessageDelayed(TYPE_HINT_PASSWORD,1000); |
|||
if(time>=30){ |
|||
binding.layoutPass.setVisibility(View.GONE); |
|||
removeMessages(TYPE_HINT_PASSWORD); |
|||
} |
|||
time++; |
|||
break; |
|||
case TYPE_START_SERVER: |
|||
removeMessages(TYPE_REFURBISH_WEBVIEW); |
|||
sendEmptyMessageDelayed(TYPE_REFURBISH_WEBVIEW,2000); |
|||
startService(new Intent(WebViewActivity.this, CoreService.class)); |
|||
LoggerUtil.e("WebViewActivity","CoreService重启"); |
|||
break; |
|||
} |
|||
} |
|||
}; |
|||
|
|||
@Override |
|||
protected void initView() { |
|||
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); |
|||
|
|||
String jsonString = FileUtil.readLocalJsonForStorage(this, StringUtil.strSplice(Constant.CACHE_PATH3,"/JSON/getChatManVideo.json")); |
|||
if(!StringUtil.isEmpty(jsonString)){ //是AI
|
|||
// 设置背景透明度
|
|||
binding.web.setBackgroundColor(Color.TRANSPARENT); |
|||
isChatMan = true; |
|||
try { |
|||
JSONObject jsonObject = new JSONObject(jsonString); |
|||
JSONArray dataArray = jsonObject.getJSONArray("data"); |
|||
for (int i = 0; i < dataArray.length(); i++) { |
|||
JSONObject videoObj = dataArray.getJSONObject(i).getJSONObject("content"); |
|||
String vType = videoObj.getString("type"); |
|||
Uri videoUri = Uri.parse(StringUtil.strSplice(Constant.CACHE_PATH3,videoObj.getJSONArray("url").getString(0))); |
|||
|
|||
VideoView video = new VideoView(this); |
|||
video.setId(View.generateViewId()); |
|||
FrameLayout.LayoutParams videoParams = new FrameLayout.LayoutParams( |
|||
ViewGroup.LayoutParams.MATCH_PARENT, // 宽度匹配父布局
|
|||
ViewGroup.LayoutParams.MATCH_PARENT // 高度匹配父布局
|
|||
); |
|||
video.setLayoutParams(videoParams); |
|||
if("bg".equals(vType)){ |
|||
currentVideo = video; |
|||
proVideo = video; |
|||
video.setVisibility(View.VISIBLE); |
|||
}else{ |
|||
video.setVisibility(View.GONE); |
|||
} |
|||
binding.videoLayout.addView(video); |
|||
video.setVideoURI(videoUri); |
|||
if("chatback".equals(vType) || "chatto".equals(vType)){ |
|||
video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { |
|||
@Override |
|||
public void onPrepared(MediaPlayer mp) { |
|||
video.start(); |
|||
} |
|||
}); |
|||
video.setOnErrorListener(new MediaPlayer.OnErrorListener() { |
|||
@Override |
|||
public boolean onError(MediaPlayer mp, int what, int extra) { |
|||
return true; // 返回true表示已处理,不会弹出系统默认提示
|
|||
} |
|||
}); |
|||
video.setOnCompletionListener( mp -> changeVideo("bg")); |
|||
}else{ |
|||
video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { |
|||
@Override |
|||
public void onPrepared(MediaPlayer mp) { |
|||
mp.setLooping(true); // 可选:设置循环播
|
|||
video.start(); |
|||
} |
|||
}); |
|||
video.setOnErrorListener(new MediaPlayer.OnErrorListener() { |
|||
@Override |
|||
public boolean onError(MediaPlayer mp, int what, int extra) { |
|||
return true; |
|||
} |
|||
}); |
|||
} |
|||
videoMap.put(vType, video); |
|||
} |
|||
}catch (JSONException e){ |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
|
|||
|
|||
//设置密码
|
|||
initPass(); |
|||
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, 0.2f, 0.2f, 1, -1, 1.0f);
|
|||
// }
|
|||
// }
|
|||
// });
|
|||
// }
|
|||
|
|||
// private void stopMusic(){
|
|||
// soundPool.stop(soundId);
|
|||
// soundPool.unload(soundId);
|
|||
// }
|
|||
|
|||
//切换视频状态
|
|||
private void changeVideoAfter(){ |
|||
if("".equals(videoAfter)){ //新状态正常
|
|||
changeVideo("back"); |
|||
}else{ //新状态彩蛋
|
|||
changeVideo("to"); |
|||
} |
|||
} |
|||
|
|||
//切换视频
|
|||
public void changeVideo(String type){ |
|||
LoggerUtil.e("切换视频", type.trim()+" isChatMan: "+String.valueOf(isChatMan)); |
|||
if(!isChatMan){ |
|||
return; |
|||
} |
|||
VideoView nextVideo; |
|||
currentVideo.clearAnimation(); |
|||
proVideo.clearAnimation(); |
|||
|
|||
switch(type.trim()){ |
|||
case "back" : |
|||
nextVideo = videoMap.get("chatback"); |
|||
break; |
|||
case "to" : |
|||
nextVideo = videoMap.get("chatto"); |
|||
break; |
|||
case "bg" : |
|||
nextVideo = "".equals(videoAfter) ? videoMap.get("bg") : videoMap.get("bg1"); |
|||
break; |
|||
case "answer" : |
|||
nextVideo = "".equals(videoAfter) ? videoMap.get("speek") : videoMap.get("speek1"); |
|||
break; |
|||
case "hello" : |
|||
nextVideo = "".equals(videoAfter) ? videoMap.get("hello") : videoMap.get("hello1"); |
|||
break; |
|||
default: |
|||
nextVideo = "".equals(videoAfter) ? videoMap.get("bg") : videoMap.get("bg1"); |
|||
} |
|||
// 检查是否是重复切换
|
|||
if (nextVideo == currentVideo) { |
|||
return; |
|||
} |
|||
|
|||
proVideo = currentVideo; |
|||
proVideo.pause(); |
|||
proVideo.stopPlayback(); |
|||
// 开始淡出动画
|
|||
proVideo.animate() |
|||
.alpha(0.5f) |
|||
.setDuration(240) |
|||
.setListener(new AnimatorListenerAdapter() { |
|||
@Override |
|||
public void onAnimationEnd(Animator animation) { |
|||
super.onAnimationEnd(animation); |
|||
proVideo.setVisibility(View.GONE); |
|||
proVideo.setAlpha(1.0f); |
|||
} |
|||
@Override |
|||
public void onAnimationCancel(Animator animation) { |
|||
super.onAnimationCancel(animation); |
|||
proVideo.setVisibility(View.GONE); |
|||
proVideo.setAlpha(1.0f); |
|||
} |
|||
}) |
|||
.start(); |
|||
|
|||
nextVideo.setAlpha(0.3f); |
|||
nextVideo.setVisibility(View.VISIBLE); |
|||
nextVideo.start(); |
|||
currentVideo = nextVideo; |
|||
|
|||
// 开始淡入动画
|
|||
currentVideo.animate() |
|||
.alpha(1.0f) |
|||
.setDuration(300) |
|||
.setListener(new AnimatorListenerAdapter() { |
|||
@Override |
|||
public void onAnimationEnd(Animator animation) { |
|||
super.onAnimationEnd(animation); |
|||
currentVideo.seekTo(0); |
|||
currentVideo.start(); |
|||
currentVideo.setAlpha(1.0f); |
|||
} |
|||
@Override |
|||
public void onAnimationCancel(Animator animation) { |
|||
super.onAnimationCancel(animation); |
|||
currentVideo.seekTo(0); |
|||
currentVideo.start(); |
|||
currentVideo.setAlpha(1.0f); |
|||
} |
|||
}) |
|||
.start(); |
|||
} |
|||
|
|||
@Override |
|||
protected void setData() { |
|||
Log.e("WebViewActivity", "setData"); |
|||
WebSettings webSettings = binding.web.getSettings(); |
|||
webSettings.setJavaScriptEnabled(true); |
|||
webSettings.setDomStorageEnabled(true); |
|||
webSettings.setUseWideViewPort(true); |
|||
webSettings.setLoadWithOverviewMode(true); |
|||
webSettings.setSupportZoom(false); |
|||
webSettings.setBuiltInZoomControls(false); |
|||
webSettings.setDisplayZoomControls(false); |
|||
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); |
|||
webSettings.setAllowFileAccess(true); |
|||
webSettings.setLoadsImagesAutomatically(true); |
|||
webSettings.setDefaultTextEncodingName("utf-8"); |
|||
webSettings.setUserAgentString(""); |
|||
webSettings.setMediaPlaybackRequiresUserGesture(false); // 允许自动播放
|
|||
|
|||
// 适配HTTPS/HTTP混合内容
|
|||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
|||
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE); |
|||
} |
|||
|
|||
//加载web
|
|||
currRoute = Constant.ROUTE_WEB_VIEW; |
|||
loadH5Url(getIntent().getBooleanExtra(Constant.KEY_LOAD_H5_URL, false)); |
|||
} |
|||
|
|||
|
|||
@Override |
|||
protected void setListener() { |
|||
binding.web.setWebViewClient(new WebViewClient() { |
|||
@Override |
|||
public boolean shouldOverrideUrlLoading(WebView view, String url) { |
|||
view.loadUrl(url); |
|||
return true; |
|||
} |
|||
|
|||
}); |
|||
binding.web.setOnLongClickListener((View v) -> { |
|||
LoggerUtil.e("OnLongClick", "用户长按了页面"); |
|||
return false; |
|||
}); |
|||
|
|||
binding.setting.setOnClickListener((view) -> { |
|||
|
|||
if (!MyApplication.addClickTimes()) return; |
|||
binding.layoutPass.setVisibility(View.VISIBLE); |
|||
time = 0; |
|||
handler.removeMessages(TYPE_HINT_PASSWORD); |
|||
handler.sendEmptyMessage(TYPE_HINT_PASSWORD); |
|||
binding.tvPass.setText(""); |
|||
// ARouter.getInstance().build(Constant.ROUTE_DEVICE_INFO).withBoolean(Constant.INPUT_PASSWORD, true).navigation();
|
|||
}); |
|||
} |
|||
|
|||
//密码输入框
|
|||
public void initPass(){ |
|||
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 5); |
|||
KeyAdapter keyAdapter = new KeyAdapter(this); |
|||
binding.recycKey.setLayoutManager(gridLayoutManager); |
|||
binding.recycKey.setAdapter(keyAdapter); |
|||
|
|||
keyAdapter.setOnitemClickLintener(new KeyAdapter.OnitemClick() { |
|||
@Override |
|||
public void onItemClick(String position) { |
|||
time = 0; |
|||
if(position.equals("清空")){ |
|||
binding.tvPass.setText(""); |
|||
}else if(position.equals("删除")){ |
|||
String s = binding.tvPass.getText().toString(); |
|||
if(s.length()>0){ |
|||
binding.tvPass.setText( s.substring(0,s.length()-1)); |
|||
}else { |
|||
binding.tvPass.setText(""); |
|||
} |
|||
}else if(position.equals("取消")){ |
|||
binding.layoutPass.setVisibility(View.GONE); |
|||
}else if(position.equals("确定")){ |
|||
SimpleDateFormat format = new SimpleDateFormat("ddMM"); |
|||
String pwd = format.format(System.currentTimeMillis());//当前日期当前月份
|
|||
StringBuilder sb = new StringBuilder(pwd); |
|||
String whatDay4 = CalendarUtils.getWhatDay4(System.currentTimeMillis()); |
|||
pwd = sb.insert(2, whatDay4).toString();//密码为日星期月6位数
|
|||
String inputPWD = binding.tvPass.getText().toString().trim();//输入的密码
|
|||
if (!pwd.equals(inputPWD)) { |
|||
ToastUtils.showToast("密码错误"); |
|||
return; |
|||
} |
|||
binding.layoutPass.setVisibility(View.GONE); |
|||
ARouter.getInstance().build(Constant.ROUTE_DEVICE_INFO).withBoolean(Constant.INPUT_PASSWORD, false).navigation(); |
|||
finish(); |
|||
}else { |
|||
binding.tvPass.setText(binding.tvPass.getText().toString()+position); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
|
|||
@Override |
|||
protected void onResume() { |
|||
super.onResume(); |
|||
leaveScreenSave(); |
|||
binding.layoutPass.setVisibility(View.GONE); |
|||
//判断web服务是否正常
|
|||
if(!Constant.isCoreService || !DeviceUtil.isServiceRunning( MyApplication.getInstance(),"CoreService")){ |
|||
LoggerUtil.e("WebViewActivity","CoreService被关闭"); |
|||
//开启本地web服务器
|
|||
stopService(new Intent(WebViewActivity.this, CoreService.class)); |
|||
handler.removeMessages(TYPE_START_SERVER); |
|||
handler.sendEmptyMessageDelayed(TYPE_START_SERVER,1000); |
|||
return; |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
protected void onPause() { |
|||
super.onPause(); |
|||
KeyboardUtil.hideKeyboard(binding.getRoot()); |
|||
} |
|||
|
|||
@Override |
|||
protected void onStop() { |
|||
super.onStop(); |
|||
if(handler!=null){ |
|||
handler.removeMessages(TYPE_HINT_PASSWORD); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
protected void onDestroy() { |
|||
super.onDestroy(); |
|||
LoggerUtil.e(TAG, "onDestroy()"); |
|||
} |
|||
|
|||
private void loadH5Url(boolean loadUrl) { |
|||
if (!loadUrl) return; |
|||
String Url; |
|||
if(H5Data.getH5Url().isEmpty()){ |
|||
Url = HtmlUrl; |
|||
}else { |
|||
Url = H5Data.getH5Url(); |
|||
} |
|||
binding.web.clearCache(true); |
|||
binding.web.loadUrl(Url); |
|||
binding.web.setWebChromeClient(new WebChromeClient() { |
|||
|
|||
public boolean onConsoleMessage(ConsoleMessage cm) { |
|||
if(!cm.message().contains("THREE.WebGLRenderer:") && !cm.message().contains("Uncaught (in promise) AbortError")){ |
|||
LoggerUtil.e("WebView日志",cm.message()); |
|||
} |
|||
return super.onConsoleMessage(cm); |
|||
} |
|||
}); |
|||
binding.web.addJavascriptInterface(new AndroidtoJs(), "android"); |
|||
} |
|||
// JS 调用 android
|
|||
public class AndroidtoJs extends Object { |
|||
// 回到屏保页面
|
|||
@JavascriptInterface |
|||
public void goScreenSave() { |
|||
LoggerUtil.e(TAG,"JS调用了Android的方法:goScreenSave()"); |
|||
if(handler!=null){ |
|||
handler.removeMessages(TYPE_GO_SAVESCREEN); |
|||
handler.sendEmptyMessage(TYPE_GO_SAVESCREEN); |
|||
} |
|||
} |
|||
// 获取是否有屏保数据
|
|||
@JavascriptInterface |
|||
public boolean hasProgram() { |
|||
boolean programPlay = ScreenSaverData.isProgramPlay(); |
|||
LoggerUtil.e(TAG,"JS调用了Android的方法:hasProgram():"+programPlay); |
|||
return programPlay; |
|||
} |
|||
// 获取后台地址
|
|||
@JavascriptInterface |
|||
public String getBaseAddress() { |
|||
LoggerUtil.e(TAG,"JS调用了Android的方法:getBaseAddress()"); |
|||
return Constant.getBaseAddress(); |
|||
} |
|||
// h5调用android:重启webView
|
|||
@JavascriptInterface |
|||
public void restartWebView() { |
|||
LoggerUtil.e(TAG,"JS调用了Android的方法:restartWebView()"); |
|||
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()"); |
|||
ttsUtil.stopTTs(); |
|||
stopAsr(); |
|||
if(mEngine == null){ |
|||
LoggerUtil.d(TAG, "未初始化"); |
|||
initASR(); |
|||
} |
|||
mEngine.start(aiCloudASRIntent); |
|||
runOnUiThread(new Runnable() { |
|||
@Override |
|||
public void run() { |
|||
changeVideo("bg"); |
|||
} |
|||
}); |
|||
} |
|||
// h5调用android:停止识别语音
|
|||
@JavascriptInterface |
|||
public void stopRecord() { |
|||
LoggerUtil.e(TAG,"JS调用了Android的方法:stopRecord()"); |
|||
stopAsr(); |
|||
} |
|||
// h5调用android:开始合成语音
|
|||
@JavascriptInterface |
|||
public void startTTS(String txt) { |
|||
LoggerUtil.e(TAG,"JS调用了Android的方法:startTTS()"); |
|||
ttsUtil.startTTs(txt.toLowerCase(Locale.ENGLISH)); |
|||
} |
|||
// h5调用android:停止合成语音
|
|||
@JavascriptInterface |
|||
public void stopTTS() { |
|||
LoggerUtil.e(TAG,"JS调用了Android的方法:stopTTS()"); |
|||
ttsUtil.stopTTs(); |
|||
runOnUiThread(new Runnable() { |
|||
@Override |
|||
public void run() { |
|||
changeVideo("bg"); |
|||
} |
|||
}); |
|||
} |
|||
@JavascriptInterface |
|||
public void stopTTS(String str) { |
|||
LoggerUtil.e(TAG,"JS调用了Android的方法:stopTTS()"); |
|||
ttsUtil.stopTTs(); |
|||
if("change".equals(str)){ |
|||
runOnUiThread(new Runnable() { |
|||
@Override |
|||
public void run() { |
|||
changeVideo("bg"); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
// h5调用android:播放指定视频
|
|||
@JavascriptInterface |
|||
public void playAudio(String str) { |
|||
LoggerUtil.e("JS调用了Android的方法:playAudio", str); |
|||
runOnUiThread(new Runnable() { |
|||
@Override |
|||
public void run() { |
|||
changeVideo(str); |
|||
} |
|||
}); |
|||
} |
|||
@JavascriptInterface |
|||
public void jxbData(String data) { |
|||
LoggerUtil.e(TAG,"JS调用了Android的方法:jxbData()"); |
|||
DeviceData.saveDeviceInfo(DeviceData.JXB_DATA,data); |
|||
} |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 安卓调用h5方法通知进入导视 |
|||
* */ |
|||
public void leaveScreenSave(){ |
|||
if(binding.web!=null){ |
|||
binding.web.loadUrl(StringUtil.strSplice("javascript:leaveScreenSave();")); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onMessageEvent(MessageEvent message) { |
|||
super.onMessageEvent(message); |
|||
switch (message.getCode()) { |
|||
case Constant.ACTION_UPDATE_WEBVIEW://刷新webView
|
|||
String Url; |
|||
if(H5Data.getH5Url().isEmpty()){ |
|||
Url = HtmlUrl; |
|||
}else { |
|||
Url = H5Data.getH5Url(); |
|||
} |
|||
binding.web.clearCache(true); |
|||
binding.web.loadUrl(Url); |
|||
ToastUtils.showToast("应用更新成功"); |
|||
break; |
|||
|
|||
case Constant.ACTION_RESTART_CORESERVICE://重启导视服务
|
|||
try { |
|||
LoggerUtil.e(TAG,"重启导视服务"); |
|||
stopService(new Intent(WebViewActivity.this, CoreService.class)); |
|||
handler.removeMessages(TYPE_START_SERVER); |
|||
handler.sendEmptyMessageDelayed(TYPE_START_SERVER,1000); |
|||
}catch (Exception e){ |
|||
|
|||
} |
|||
break; |
|||
case Constant.VOID_STOP: |
|||
changeVideo("bg"); |
|||
binding.web.loadUrl(StringUtil.strSplice("javascript:ttsPlayOver();")); |
|||
break; |
|||
case Constant.ACTION_UPDATE_PROG2://后台下发节目通知,重置导视倒计时。
|
|||
LoggerUtil.e(TAG,"重启导视倒计时"); |
|||
leaveScreenSave(); |
|||
break; |
|||
} |
|||
} |
|||
|
|||
//--------
|
|||
private void initASR(){ |
|||
if(mEngine == null && MyApplication.getInstance().duiliteConfig){ |
|||
AICloudASRConfig config = new AICloudASRConfig(); |
|||
config.setLocalVadEnable(true); |
|||
config.setVadResource("vad_aihome_v0.11.bin"); |
|||
mEngine = AICloudASREngine.createInstance(); |
|||
aiCloudASRIntent = new AICloudASRIntent(); |
|||
aiCloudASRIntent.setEnablePunctuation(true); |
|||
aiCloudASRIntent.setRealback(true);//打开实时反馈功能
|
|||
aiCloudASRIntent.setResourceType(Constant.asrModel); |
|||
aiCloudASRIntent.setEnableNumberConvert(true);//设置启用识别结果汉字数字转阿拉伯数字功能
|
|||
aiCloudASRIntent.setWaitingTimeout(30000); //设置等待识别结果超时时长,默认5000ms
|
|||
aiCloudASRIntent.setNoSpeechTimeOut(10000); |
|||
aiCloudASRIntent.setPauseTime(2000); |
|||
mEngine.init(config, new AIASRListenerImpl()); |
|||
} |
|||
} |
|||
|
|||
private class AIASRListenerImpl implements AIASRListener { |
|||
|
|||
public void onError(AIError error) { |
|||
LoggerUtil.e("ASRInit: ","onError "+error.getMessage()); |
|||
// 在非主线程中需要调用 WebView 方法时
|
|||
runOnUiThread(new Runnable() { |
|||
@Override |
|||
public void run() { |
|||
binding.web.evaluateJavascript("javascript: window.youAskOver('');", null); |
|||
} |
|||
}); |
|||
stopAsr(); |
|||
} |
|||
|
|||
public void onResults(AIResult results) { |
|||
LoggerUtil.e("ASRInit: ", "onResults "+String.valueOf(results.isLast())); |
|||
if (results.getResultType() == AIConstant.AIENGINE_MESSAGE_TYPE_JSON) { |
|||
JSONResultParser parser = new JSONResultParser((String) results.getResultObject()); |
|||
if("".equals(parser.getVar()) && !"".equals(parser.getText())){ |
|||
oldString = StringUtil.strSplice(oldString,parser.getText()); |
|||
} |
|||
if(!"".equals(parser.getVar())){ |
|||
voiceContent( StringUtil.strSplice(oldString,parser.getVar())); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onInit(int status) { |
|||
if (status == AIConstant.OPT_SUCCESS) { |
|||
LoggerUtil.e("ASRInit: ","初始化成功!"); |
|||
} else { |
|||
LoggerUtil.e("ASRInit: ","初始化失败!code:" + status); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onBeginningOfSpeech() { |
|||
//本地vad打开时,才会执行
|
|||
oldString=""; |
|||
LoggerUtil.e("ASRInit: ","onBeginningOfSpeech"); |
|||
} |
|||
|
|||
@Override |
|||
public void onNotOneShot() { |
|||
//do nothing
|
|||
LoggerUtil.e("ASRInit: ","onNotOneShot"); |
|||
} |
|||
|
|||
@Override |
|||
public void onEndOfSpeech() { |
|||
//本地vad打开时,才会执行
|
|||
stopAsr(); |
|||
runOnUiThread(new Runnable() { |
|||
@Override |
|||
public void run() { |
|||
binding.web.evaluateJavascript("javascript: window.youAskOver('');", null); |
|||
} |
|||
}); |
|||
LoggerUtil.e("ASRInit: ","onEndOfSpeech"); |
|||
} |
|||
|
|||
@Override |
|||
public void onReadyForSpeech() { |
|||
oldString=""; |
|||
LoggerUtil.e("ASRInit: ","onReadyForSpeech"); |
|||
} |
|||
|
|||
@Override |
|||
public void onResultDataReceived(byte[] buffer, int size, int wakeupType) { |
|||
LoggerUtil.e("ASRInit: ","onResultDataReceived "+String.valueOf(size)); |
|||
} |
|||
|
|||
@Override |
|||
public void onRmsChanged(float rmsdB) { |
|||
LoggerUtil.e("ASRInit: ", "onRmsChanged " + rmsdB); |
|||
} |
|||
|
|||
@Override |
|||
public void onResultDataReceived(byte[] buffer, int size) { |
|||
//do nothing
|
|||
} |
|||
|
|||
@Override |
|||
public void onRawDataReceived(byte[] buffer, int size) { |
|||
//do nothing
|
|||
LoggerUtil.e("ASRInit: ","onRawDataReceived "+String.valueOf(size)); |
|||
} |
|||
|
|||
} |
|||
|
|||
/** |
|||
* android调用js:传送识别语音内容 |
|||
*/ |
|||
public void voiceContent(String msg) { |
|||
if (binding.web != null) { |
|||
LoggerUtil.e(TAG, "android调用js方法:giveAskText(),语音内容:" + msg); |
|||
runOnUiThread(new Runnable() { |
|||
@Override |
|||
public void run() { |
|||
binding.web.loadUrl(StringUtil.strSplice("javascript:window.giveAskText('" + msg + "');")); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
private void stopAsr(){ |
|||
if(mEngine != null){ |
|||
mEngine.cancel(); |
|||
mEngine.stop(); |
|||
} |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue