初始化框架,完成扫描回调,语音接口
This commit is contained in:
@ -0,0 +1,27 @@
|
||||
package chaoran.business.activity;
|
||||
|
||||
/*
|
||||
**********************************************
|
||||
* DATE PERSON REASON
|
||||
* 2021-02-04 FXY Created
|
||||
**********************************************
|
||||
*/
|
||||
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.widget.TextView;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import chaoran.business.R;
|
||||
|
||||
public class AboutActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_about);
|
||||
setTitle(R.string.activity_about);
|
||||
TextView textView = findViewById(R.id.about_content);
|
||||
textView.setText(R.string.activity_about_content);
|
||||
}
|
||||
}
|
||||
128
app/src/main/java/chaoran/business/activity/MainActivity.java
Normal file
128
app/src/main/java/chaoran/business/activity/MainActivity.java
Normal file
@ -0,0 +1,128 @@
|
||||
package chaoran.business.activity;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.widget.Toast;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import chaoran.business.BrandEnum;
|
||||
import chaoran.business.R;
|
||||
import chaoran.business.adapter.*;
|
||||
import chaoran.business.vioce.TekVoiceEngine;
|
||||
import chaoran.business.vioce.VoiceEngine;
|
||||
|
||||
/**
|
||||
* 流程:联网认证设备型号,验证通过,查找设备品牌进行调用驱动操作
|
||||
*/
|
||||
|
||||
/**
|
||||
* 提供文字转语音功能,实时播报PDA状态
|
||||
*/
|
||||
public class MainActivity extends AppCompatActivity implements ResultListener {
|
||||
|
||||
private WebView webView;
|
||||
private Adapter adapter;
|
||||
private VoiceEngine voiceEngine;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
initView();
|
||||
initData();
|
||||
}
|
||||
|
||||
private void initData() {
|
||||
BrandEnum brand = BrandEnum.code(Build.MANUFACTURER);
|
||||
Toast.makeText(this, Build.MANUFACTURER, Toast.LENGTH_LONG).show();
|
||||
switch (brand) {
|
||||
case UROBO:
|
||||
adapter = new UroBoAdapter(this, this);
|
||||
break;
|
||||
case ROCKCHIP:
|
||||
adapter = new RockChipAdapter(this, this);
|
||||
break;
|
||||
case IDATA:
|
||||
adapter = new IDataAdapter(this, this);
|
||||
break;
|
||||
case ALPS:
|
||||
adapter = new AlpsAdapter(this, this);
|
||||
break;
|
||||
}
|
||||
if (null != adapter) {
|
||||
adapter.start();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("JavascriptInterface")
|
||||
private void initView() {
|
||||
setTitle(R.string.title_activity_main);
|
||||
voiceEngine = new TekVoiceEngine(this);
|
||||
webView = findViewById(R.id.webView);
|
||||
WebSettings settings = webView.getSettings();
|
||||
settings.setJavaScriptEnabled(true);
|
||||
webView.addJavascriptInterface(voiceEngine, "VoiceEngine");
|
||||
webView.loadUrl(url());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
//再次唤醒该页面时,重新加载页面和语音配置
|
||||
webView.loadUrl(url());
|
||||
voiceEngine.loadParam();
|
||||
super.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
public void result(String result) {
|
||||
runOnUiThread(() -> webView.loadUrl("javascript:render(\'" + result + "\')"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
adapter.stop();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.menu_main, menu);
|
||||
return super.onCreateOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_setting_network:
|
||||
startActivity(new Intent(this, NetworkSettingActivity.class));
|
||||
break;
|
||||
case R.id.action_setting_voice:
|
||||
startActivity(new Intent(this, VoiceSettingActivity.class));
|
||||
break;
|
||||
case R.id.action_about:
|
||||
startActivity(new Intent(this, AboutActivity.class));
|
||||
break;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
private String url() {
|
||||
SharedPreferences spf = this.getSharedPreferences("crtech", Context.MODE_PRIVATE);
|
||||
String address = spf.getString("address", "").replaceAll(" ", "");
|
||||
String path = spf.getString("path", "").replaceAll(" ", "");
|
||||
Integer port = spf.getInt("port", -1);
|
||||
String link = address.concat(":").concat(String.valueOf(port)).concat(path);
|
||||
return link.startsWith("http://") ? link : "http://".concat(link);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
package chaoran.business.activity;
|
||||
|
||||
/*
|
||||
**********************************************
|
||||
* DATE PERSON REASON
|
||||
* 2021-02-02 FXY Created
|
||||
**********************************************
|
||||
*/
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import chaoran.business.R;
|
||||
|
||||
public class NetworkSettingActivity extends AppCompatActivity {
|
||||
|
||||
private EditText address, path, port;
|
||||
private Button save, cancel;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_setting_network);
|
||||
initView();
|
||||
}
|
||||
|
||||
|
||||
private void initView() {
|
||||
address = findViewById(R.id.address);
|
||||
path = findViewById(R.id.path);
|
||||
port = findViewById(R.id.port);
|
||||
save = findViewById(R.id.save);
|
||||
cancel = findViewById(R.id.cancel);
|
||||
setTitle(R.string.title_activity_setting_network);
|
||||
SharedPreferences sharedPreferences = this.getSharedPreferences("crtech", Context.MODE_PRIVATE);
|
||||
address.setText(sharedPreferences.getString("address", ""));
|
||||
path.setText(sharedPreferences.getString("path", ""));
|
||||
port.setText(String.valueOf(sharedPreferences.getInt("port", -1)));
|
||||
cancel.setOnClickListener((e) -> this.finish());
|
||||
save.setOnClickListener((e) -> saveSetting());
|
||||
}
|
||||
|
||||
private void saveSetting() {
|
||||
SharedPreferences.Editor editor = this.getSharedPreferences("crtech", Context.MODE_PRIVATE).edit();
|
||||
editor.putString("address", address.getText().toString().trim());
|
||||
editor.putString("path", path.getText().toString().trim());
|
||||
editor.putInt("port", Integer.parseInt(port.getText().toString().trim()));
|
||||
editor.commit();
|
||||
this.finish();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package chaoran.business.activity;
|
||||
|
||||
/*
|
||||
**********************************************
|
||||
* DATE PERSON REASON
|
||||
* 2021-02-01 FXY Created
|
||||
**********************************************
|
||||
*/
|
||||
|
||||
/**
|
||||
* 结果监听器,用于回调结果
|
||||
* 调用前端render()函数
|
||||
*/
|
||||
public interface ResultListener {
|
||||
|
||||
/**
|
||||
* js 调用方法
|
||||
*
|
||||
* @param result
|
||||
* @return
|
||||
*/
|
||||
public void result(String result);
|
||||
}
|
||||
@ -0,0 +1,137 @@
|
||||
package chaoran.business.activity;
|
||||
|
||||
/*
|
||||
**********************************************
|
||||
* DATE PERSON REASON
|
||||
* 2021-02-05 FXY Created
|
||||
**********************************************
|
||||
*/
|
||||
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.widget.*;
|
||||
import chaoran.business.R;
|
||||
import chaoran.business.vioce.TekVoiceEngine;
|
||||
import chaoran.business.vioce.VoiceEngine;
|
||||
|
||||
/**
|
||||
* 播报语音设置
|
||||
*/
|
||||
public class VoiceSettingActivity extends Activity {
|
||||
|
||||
private EditText testText;
|
||||
private RadioButton voiceF, voiceY;
|
||||
private SeekBar voiceSize, voiceSpeed, voiceIndicate;
|
||||
private Button save, cancel;
|
||||
private SharedPreferences sharedPreferences;
|
||||
private RadioGroup voiceMemberGroup;
|
||||
|
||||
private VoiceEngine voiceEngine;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_setting_voice);
|
||||
initView();
|
||||
initData();
|
||||
initListener();
|
||||
}
|
||||
|
||||
private void initView() {
|
||||
testText = findViewById(R.id.voice_test_text);
|
||||
voiceF = findViewById(R.id.voice_member_xiaofeng);
|
||||
voiceY = findViewById(R.id.voice_member_xiaoyan);
|
||||
voiceSize = findViewById(R.id.voice_size);
|
||||
voiceSpeed = findViewById(R.id.voice_speed);
|
||||
voiceIndicate = findViewById(R.id.voice_indicate);
|
||||
save = findViewById(R.id.save_voice_setting);
|
||||
cancel = findViewById(R.id.cancel_voice_setting);
|
||||
voiceMemberGroup = findViewById(R.id.voice_member_group);
|
||||
voiceEngine = new TekVoiceEngine(this);
|
||||
}
|
||||
|
||||
private void initData() {
|
||||
sharedPreferences = getSharedPreferences("voiceEngine", MODE_PRIVATE);
|
||||
//设置测试文本
|
||||
testText.setText(sharedPreferences.getString("testText", testText.getText().toString()));
|
||||
//设置播报人员
|
||||
String voiceMember = sharedPreferences.getString("voiceMember", "xiaofeng");
|
||||
if (voiceMember.equals("xiaofeng")) {
|
||||
voiceF.setChecked(true);
|
||||
voiceY.setChecked(false);
|
||||
} else {
|
||||
voiceY.setChecked(true);
|
||||
voiceF.setChecked(false);
|
||||
}
|
||||
//设置功能选项
|
||||
voiceSize.setProgress(sharedPreferences.getInt("voiceSize", 100));
|
||||
voiceSpeed.setProgress(sharedPreferences.getInt("voiceSpeed", 80));
|
||||
voiceIndicate.setProgress(sharedPreferences.getInt("voiceIndicate", 50));
|
||||
}
|
||||
|
||||
public void initListener() {
|
||||
SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {
|
||||
//停止播放
|
||||
voiceEngine.stopSpeaking();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {
|
||||
//播放语音
|
||||
voiceEngine.setParam(voiceF.isChecked() ? "xiaofeng" : "xiaoyan", voiceSize.getProgress(), voiceIndicate.getProgress(), voiceSpeed.getProgress());
|
||||
voiceEngine.startSpeaking(testText.getText().toString());
|
||||
}
|
||||
};
|
||||
voiceSize.setOnSeekBarChangeListener(seekBarChangeListener);
|
||||
voiceIndicate.setOnSeekBarChangeListener(seekBarChangeListener);
|
||||
voiceSpeed.setOnSeekBarChangeListener(seekBarChangeListener);
|
||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||
save.setOnClickListener(a -> {
|
||||
editor.putInt("voiceSize", voiceSize.getProgress());
|
||||
editor.putInt("voiceIndicate", voiceIndicate.getProgress());
|
||||
editor.putInt("voiceSpeed", voiceSpeed.getProgress());
|
||||
editor.putString("testText", testText.getText().toString());
|
||||
editor.putString("voiceMember", voiceF.isChecked() ? "xiaofeng" : "xiaoyan");
|
||||
editor.commit();
|
||||
voiceEngine.stopSpeaking();
|
||||
voiceEngine.destroy();
|
||||
Toast.makeText(this, "语音配置保存成功!", Toast.LENGTH_SHORT).show();
|
||||
this.finish();
|
||||
});
|
||||
cancel.setOnClickListener(a -> {
|
||||
voiceEngine.stopSpeaking();
|
||||
voiceEngine.destroy();
|
||||
this.finish();
|
||||
});
|
||||
|
||||
voiceMemberGroup.setOnCheckedChangeListener((a, b) -> {
|
||||
//播放语音
|
||||
voiceEngine.setParam(voiceF.isChecked() ? "xiaofeng" : "xiaoyan", voiceSize.getProgress(), voiceIndicate.getProgress(), voiceSpeed.getProgress());
|
||||
voiceEngine.startSpeaking(testText.getText().toString());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
//初始化加载一次
|
||||
voiceEngine.setParam(voiceF.isChecked() ? "xiaofeng" : "xiaoyan", voiceSize.getProgress(), voiceIndicate.getProgress(), voiceSpeed.getProgress());
|
||||
voiceEngine.startSpeaking(testText.getText().toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
voiceEngine.stopSpeaking();
|
||||
voiceEngine.destroy();
|
||||
super.onDestroy();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user