mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-21 07:40:37 +08:00
[Android] add VoiceAssistant app example (#834)
* [Android]add VoiceAssistant. * Create VoiceAssistantDemo * Update and rename VoiceAssistantDemo to VoiceAssistantDemo.md * Update VoiceAssistantDemo.md * Delete VoiceAssistantDemo.md * [Android]1.delete about core folder. 2.build and configure bdasr_V3_20210628_cfe8c44.aar file. * change app/build.gradle etc. * Update build.gradle Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com>
This commit is contained in:
@@ -5,7 +5,7 @@ android {
|
||||
|
||||
defaultConfig {
|
||||
applicationId 'com.baidu.paddle.fastdeploy.app.examples'
|
||||
minSdkVersion 15
|
||||
minSdkVersion 16
|
||||
//noinspection ExpiredTargetSdkVersion
|
||||
targetSdkVersion 28
|
||||
versionCode 1
|
||||
@@ -79,6 +79,10 @@ def FD_JAVA_SDK = [
|
||||
[
|
||||
'src' : 'https://bj.bcebos.com/fastdeploy/test/fastdeploy-android-sdk-latest-dev.aar',
|
||||
'dest': 'libs'
|
||||
],
|
||||
[
|
||||
'src' : 'https://bj.bcebos.com/fastdeploy/test/bdasr_V3_20210628_cfe8c44.aar',
|
||||
'dest': 'libs'
|
||||
]
|
||||
]
|
||||
|
||||
@@ -132,7 +136,7 @@ task downloadAndExtractSDKs(type: DefaultTask) {
|
||||
boolean copyFiles = false
|
||||
if (!file("${sdk.dest}/${sdkName}").exists()) {
|
||||
// Download the target SDK if not exists
|
||||
if (file("${cachePath}/${sdkName}").exists()) {
|
||||
if (!file("${cachePath}/${sdkName}").exists()) {
|
||||
println "[INFO] Downloading ${sdk.src} -> ${cachePath}/${sdkName}"
|
||||
ant.get(src: sdk.src, dest: file("${cachePath}/${sdkName}"))
|
||||
}
|
||||
|
@@ -0,0 +1,211 @@
|
||||
package com.baidu.paddle.fastdeploy.app.examples.ernie.applications;
|
||||
|
||||
import static com.baidu.paddle.fastdeploy.ui.Utils.isNetworkAvailable;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.Button;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.baidu.aip.asrwakeup3.core.mini.AutoCheck;
|
||||
import com.baidu.aip.asrwakeup3.core.util.AuthUtil;
|
||||
import com.baidu.paddle.fastdeploy.app.examples.R;
|
||||
import com.baidu.speech.EventListener;
|
||||
import com.baidu.speech.EventManager;
|
||||
import com.baidu.speech.EventManagerFactory;
|
||||
import com.baidu.speech.asr.SpeechConstant;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class VoiceAssistantMainActivity extends Activity implements View.OnClickListener, EventListener {
|
||||
private Button startVoiceBtn;
|
||||
private TextView voiceOutput;
|
||||
private Button startIntentBtn;
|
||||
private TextView intentOutput;
|
||||
private ImageView back;
|
||||
private EventManager asr;
|
||||
private Boolean isStartVoice = false;
|
||||
private String voiceTxt = "";
|
||||
private int times = 0;
|
||||
private final int REQUEST_PERMISSION = 0;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
// Fullscreen
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
|
||||
setContentView(R.layout.voice_assistant_activity_main);
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
checkPermission();
|
||||
asr = EventManagerFactory.create(this, "asr");
|
||||
asr.registerListener(this);
|
||||
startVoiceBtn = findViewById(R.id.btn_voice);
|
||||
startVoiceBtn.setOnClickListener(this);
|
||||
voiceOutput = findViewById(R.id.tv_voice_output);
|
||||
back = findViewById(R.id.iv_back);
|
||||
back.setOnClickListener(this);
|
||||
startIntentBtn = findViewById(R.id.btn_intent);
|
||||
startIntentBtn.setOnClickListener(this);
|
||||
intentOutput = findViewById(R.id.tv_intent_output);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
switch (view.getId()) {
|
||||
case R.id.btn_voice:
|
||||
if (!isNetworkAvailable(this)) {
|
||||
new AlertDialog.Builder(VoiceAssistantMainActivity.this)
|
||||
.setMessage("请先连接互联网。")
|
||||
.setCancelable(true)
|
||||
.show();
|
||||
return;
|
||||
}
|
||||
if (!isStartVoice) {
|
||||
isStartVoice = true;
|
||||
startVoiceBtn.setText("停止录音");
|
||||
start();
|
||||
} else {
|
||||
isStartVoice = false;
|
||||
startVoiceBtn.setText("开始录音");
|
||||
stop();
|
||||
}
|
||||
break;
|
||||
case R.id.iv_back:
|
||||
finish();
|
||||
break;
|
||||
case R.id.btn_intent:
|
||||
if (voiceTxt.equals("")) {
|
||||
new AlertDialog.Builder(VoiceAssistantMainActivity.this)
|
||||
.setMessage("请先录音。")
|
||||
.setCancelable(true)
|
||||
.show();
|
||||
return;
|
||||
}
|
||||
intentOutput.setText("我刚才说了:" + voiceTxt);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEvent(String name, String params, byte[] data, int offset, int length) {
|
||||
if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL)) {
|
||||
if (params.contains("\"final_result\"")) {
|
||||
if (params.contains("[")) {
|
||||
voiceTxt = params.substring(params.lastIndexOf('[') + 1, params.lastIndexOf(']'));
|
||||
}
|
||||
voiceOutput.setText(voiceTxt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void start() {
|
||||
Map<String, Object> params = AuthUtil.getParam();
|
||||
String event = null;
|
||||
event = SpeechConstant.ASR_START;
|
||||
params.put(SpeechConstant.ACCEPT_AUDIO_VOLUME, false);
|
||||
(new AutoCheck(getApplicationContext(), new Handler() {
|
||||
public void handleMessage(Message msg) {
|
||||
if (msg.what == 100) {
|
||||
AutoCheck autoCheck = (AutoCheck) msg.obj;
|
||||
synchronized (autoCheck) {
|
||||
String message = autoCheck.obtainErrorMessage();
|
||||
Log.e(getClass().getName(), message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, false)).checkAsr(params);
|
||||
String json = null;
|
||||
json = new JSONObject(params).toString();
|
||||
asr.send(event, json, null, 0, 0);
|
||||
}
|
||||
|
||||
private void stop() {
|
||||
asr.send(SpeechConstant.ASR_STOP, null, null, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
asr.send(SpeechConstant.ASR_CANCEL, "{}", null, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
asr.send(SpeechConstant.ASR_CANCEL, "{}", null, 0, 0);
|
||||
asr.unregisterListener(this);
|
||||
}
|
||||
|
||||
private void checkPermission() {
|
||||
times++;
|
||||
final List<String> permissionsList = new ArrayList<>();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
if ((checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED))
|
||||
permissionsList.add(Manifest.permission.RECORD_AUDIO);
|
||||
if ((checkSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED))
|
||||
permissionsList.add(Manifest.permission.ACCESS_NETWORK_STATE);
|
||||
if ((checkSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED)) {
|
||||
permissionsList.add(Manifest.permission.INTERNET);
|
||||
}
|
||||
if ((checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
|
||||
permissionsList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
||||
}
|
||||
if (permissionsList.size() != 0) {
|
||||
if (times == 1) {
|
||||
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
|
||||
REQUEST_PERMISSION);
|
||||
} else {
|
||||
new AlertDialog.Builder(this)
|
||||
.setCancelable(true)
|
||||
.setTitle("提示")
|
||||
.setMessage("获取不到授权,APP将无法正常使用,请允许APP获取权限!")
|
||||
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface arg0, int arg1) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
|
||||
REQUEST_PERMISSION);
|
||||
}
|
||||
}
|
||||
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface arg0, int arg1) {
|
||||
finish();
|
||||
}
|
||||
}).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
checkPermission();
|
||||
}
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
package com.baidu.paddle.fastdeploy.app.examples.ernie.applications;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Color;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.View;
|
||||
|
||||
import com.baidu.paddle.fastdeploy.app.examples.R;
|
||||
|
||||
public class VoiceAssistantWelcomeActivity extends Activity {
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
|
||||
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
||||
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
||||
);
|
||||
getWindow().setStatusBarColor(Color.TRANSPARENT);
|
||||
}
|
||||
setContentView(R.layout.voice_assistant_welcome);
|
||||
}
|
||||
|
||||
public void startActivity(View view) {
|
||||
Intent intent = new Intent(VoiceAssistantWelcomeActivity.this, VoiceAssistantMainActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
}
|
@@ -0,0 +1,89 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<com.baidu.paddle.fastdeploy.ui.layout.ActionBarLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/iv_back"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:cropToPadding="true"
|
||||
android:paddingLeft="40px"
|
||||
android:paddingTop="60px"
|
||||
android:paddingRight="60px"
|
||||
android:paddingBottom="40px"
|
||||
android:src="@drawable/back_btn" />
|
||||
|
||||
<TextView
|
||||
style="@style/action_btn"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginTop="50px"
|
||||
android:text="@string/voice_assistant_app_name"
|
||||
android:textSize="15sp" />
|
||||
</com.baidu.paddle.fastdeploy.ui.layout.ActionBarLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical">
|
||||
|
||||
<Button
|
||||
android:id="@+id/btn_voice"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:text="开始录音" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="20dp"
|
||||
android:layout_marginTop="20dp"
|
||||
android:layout_marginRight="20dp"
|
||||
android:text="语音转文字结果:" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_voice_output"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="20dp"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginRight="20dp"
|
||||
android:textColor="@android:color/black"
|
||||
android:textSize="20sp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btn_intent"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:layout_marginTop="20dp"
|
||||
android:text="开始意图识别" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="20dp"
|
||||
android:layout_marginTop="20dp"
|
||||
android:layout_marginRight="20dp"
|
||||
android:text="意图识别结果:" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_intent_output"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="20dp"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginRight="20dp"
|
||||
android:textColor="@android:color/black"
|
||||
android:textSize="20sp" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
@@ -0,0 +1,76 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/imageView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:scaleType="centerCrop"
|
||||
android:background="@drawable/main_bk"
|
||||
/>
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/model_text"
|
||||
android:layout_width="320dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginTop="120dp"
|
||||
android:background="@color/colorStartBtn"
|
||||
android:gravity="center"
|
||||
android:text="VoiceAssistant"
|
||||
android:textColor="@color/colorTextWrite"
|
||||
android:textSize="30sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/baidu"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/model_text"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginTop="20dp"
|
||||
android:text="百度FastDeploy"
|
||||
android:textColor="@color/colorTextWrite"
|
||||
android:textSize="22sp" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/baidu"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginTop="10dp"
|
||||
android:text="Powered by EasyEdge"
|
||||
android:textColor="@color/colorTextWrite"
|
||||
android:textSize="18sp" />
|
||||
|
||||
<Button
|
||||
|
||||
android:id="@+id/start_ui_activity"
|
||||
android:layout_width="200dp"
|
||||
android:layout_height="50dp"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginBottom="70dp"
|
||||
android:background="@drawable/round_corner_btn"
|
||||
android:text="@string/start_ui_activity"
|
||||
android:textColor="@color/colorTextWrite"
|
||||
android:textSize="22sp"
|
||||
android:onClick="startActivity"/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/logo"
|
||||
android:layout_width="95dp"
|
||||
android:layout_height="30dp"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:background="@drawable/paddle_logo"
|
||||
android:scaleType="centerCrop" />
|
||||
</RelativeLayout>
|
||||
</FrameLayout>
|
@@ -1,4 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.baidu.paddle.fastdeploy.ui">
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
</manifest>
|
@@ -6,6 +6,8 @@ import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.hardware.Camera;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.Uri;
|
||||
import android.opengl.GLES20;
|
||||
import android.os.Environment;
|
||||
@@ -310,4 +312,28 @@ public class Utils {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isNetworkAvailable(final Context context) {
|
||||
boolean hasWifoCon = false;
|
||||
boolean hasMobileCon = false;
|
||||
|
||||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo[] netInfos = cm.getAllNetworkInfo();
|
||||
for (NetworkInfo net : netInfos) {
|
||||
|
||||
String type = net.getTypeName();
|
||||
if (type.equalsIgnoreCase("WIFI")) {
|
||||
if (net.isConnected()) {
|
||||
hasWifoCon = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (type.equalsIgnoreCase("MOBILE")) {
|
||||
if (net.isConnected()) {
|
||||
hasMobileCon = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return hasWifoCon || hasMobileCon;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user