forked from PaddlePaddle/FastDeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android] add VoiceAssistant app example (PaddlePaddle#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 <[email protected]>
- Loading branch information
1 parent
c7dc7d5
commit e877f0f
Showing
7 changed files
with
441 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
...m/baidu/paddle/fastdeploy/app/examples/ernie/applications/VoiceAssistantMainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...aidu/paddle/fastdeploy/app/examples/ernie/applications/VoiceAssistantWelcomeActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
java/android/app/src/main/res/layout/voice_assistant_activity_main.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
Oops, something went wrong.