语音搜索。
先实现搜索界面。
sh.jpg
定义输入框:search_page.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape_edittext"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<EditText
android:id="@+id/searchEd"
android:drawableLeft="@drawable/search_gray"
android:background="@null"
android:layout_width="270dp"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/speak"
android:src="@drawable/audio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:text="搜索"
android:textColor="@android:color/holo_blue_dark"
android:layout_marginTop="23dp"
android:paddingLeft="5dp"
android:textSize="17dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
这个搜索框可以手动输入,也可以点击语音图标,用百度语音识别结果搜索。
speak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(context,SpeakPageActivity.class),1);
//fragment界面执行startActivityForResult,前面不要加context,加了context
//onActivityResult会不执行,应该是执行到context那个onActivityResult里面了
}
});
这里专门创建了一个SpeakPageActivity来处理语音。
bd.jpg
speak_page.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="15dp"
android:text="你可以这样说\n\n\n故宫门票\n北京一日游\n迪士尼乐园"
android:layout_height="wrap_content"/>
<FrameLayout
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
>
<ImageView
android:id="@+id/audio_button"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="@drawable/shape_circle" />
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center"
android:background="@drawable/audio_white" />
</FrameLayout>
<ImageView
android:id="@+id/audio_cancel"
android:background="@drawable/audio_cancel"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="50dp"
android:layout_marginRight="50dp"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
点击audioButton,执行动画:
private void setAnim() {
AnimationSet as = new AnimationSet(true);
//缩放动画,以中心从原始放大到0.5倍
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
//渐变动画
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.5f);
scaleAnimation.setDuration(1000);
scaleAnimation.setRepeatCount(Animation.INFINITE);
alphaAnimation.setRepeatCount(Animation.INFINITE);
as.setDuration(1000);
as.addAnimation(scaleAnimation);
as.addAnimation(alphaAnimation);
as.setRepeatMode(Animation.REVERSE);//重复反向执行
audioButton.startAnimation(as);
}
并且开始录音:
Map<String, Object> params = new LinkedHashMap<String, Object>();
params.put(SpeechConstant.ACCEPT_AUDIO_VOLUME, false);
asrManager.start(params);
当说话停止,或者手指松开,都会停止录音并识别:
else if(event.getAction() == MotionEvent.ACTION_UP){
asrManager.stop();
audioButton.clearAnimation();//关掉动画。
}
识别到结果后,传到SearchPageActivity界面。
public void onAsrFinalResult(String[] results, RecogResult recogResult) {
audioButton.clearAnimation();
String message = results[0];
Log.v("aaaa","--"+message);
if(message != null) {
Intent intent = new Intent();
intent.putExtra("result", message);
setResult(3, intent);
}
SpeakPageActivity.this.finish();
}
销毁语音界面,并执行百度语音的release()方法。
protected void onDestroy() {
asrManager.release();
super.onDestroy();
}
SearchPageActivity接受数据。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.v("nn","nn");
super.onActivityResult(requestCode, resultCode, data);
if(data != null) {
String str = data.getStringExtra("result");
searchEd.setText(str);
}
}
代码如下:
https://github.com/doudousang/baidusearch.git
参考代码:
一看就懂的 startActivityForResult
谈谈Fragment中的onActivityResult
关于Android Activity之间传递数据的6种方式
RelativeLayout的layout_marginBottom属性失效问题
网友评论