android 项目学习随笔二十一(IM、语音识别、机器人、统计、扫描二维码、条形码)

 语音识别科大讯飞语音云 http://www.xfyun.cn/

 

语音机器人模拟

public class TalkBean {

    public String text;
    public boolean isAsk;// true表示提问,false是回答
    public int imageId = -1;// 回答的图片id

    public TalkBean(String text, boolean isAsk, int imageId) {
        this.text = text;
        this.isAsk = isAsk;
        this.imageId = imageId;
    }

}
TalkBean
import java.util.ArrayList;

/**
 * 语音对象封装
 * 
 */
public class VoiceBean {

    public ArrayList<WSBean> ws;

    public class WSBean {
        public ArrayList<CWBean> cw;
    }

    public class CWBean {
        public String w;
    }
}
VoiceBean
import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;
import com.itheima.mysiri66.VoiceBean.WSBean;
import com.umeng.analytics.MobclickAgent;

public class MainActivity  extends Activity {

    private ListView lvList;
    StringBuffer mBuffer = new StringBuffer();

    private String[] mAnswers = new String[] { "这张怎么样?", "约吗?", "老地方见!",
            "不要再要美女了", "最后一张了" };

    private int[] mAnswerPics = new int[] { R.drawable.p1, R.drawable.p2,
            R.drawable.p3, R.drawable.p4 };

    private ArrayList<TalkBean> mTalkList = new ArrayList<TalkBean>();// 会话集合
    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 初始化语音识别
        SpeechUtility.createUtility(this, SpeechConstant.APPID + "=55cfe490");

        setContentView(R.layout.activity_main);
        lvList = (ListView) findViewById(R.id.lv_list);
        mAdapter = new MyAdapter();
        lvList.setAdapter(mAdapter);
    }

    // 开始识别
    public void startRecognize(View view) {
        // 1.创建RecognizerDialog对象
        RecognizerDialog mDialog = new RecognizerDialog(this, null);
        // 2.设置accent、language等参数
        mDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        mDialog.setParameter(SpeechConstant.ACCENT, "mandarin");

        // 3.设置回调接口
        mDialog.setListener(new RecognizerDialogListener() {

            @Override
            public void onResult(RecognizerResult results, boolean isLast) {
                System.out.println("isLast:" + isLast);
                String parseResult = parseData(results.getResultString());
                mBuffer.append(parseResult);

                if (isLast) {
                    // 会话已经结束
                    String finalResult = mBuffer.toString();
                    System.out.println("最终结果:" + finalResult);

                    mBuffer = new StringBuffer();// 此方法相当于清空buffer

                    // 提问对象
                    TalkBean askBean = new TalkBean(finalResult, true, -1);
                    mTalkList.add(askBean);

                    Random random = new Random();

                    // 回答对象
                    String answer = "没听清";
                    int imageId = -1;
                    if (finalResult.contains("你好")) {
                        answer = "你好呀!";
                    } else if (finalResult.contains("你是谁")) {
                        answer = "我是你的小助手!";
                    } else if (finalResult.contains("天王盖地虎")) {
                        answer = "小鸡炖蘑菇";
                        imageId = R.drawable.m;
                    } else if (finalResult.contains("美女")) {
                        // 取随机内容和随机图片
                        int strId = random.nextInt(mAnswers.length);
                        answer = mAnswers[strId];
                        imageId = mAnswerPics[random
                                .nextInt(mAnswerPics.length)];
                    }

                    // 回答对象
                    TalkBean answerBean = new TalkBean(answer, false, imageId);
                    mTalkList.add(answerBean);
                    mAdapter.notifyDataSetChanged();

                    lvList.setSelection(mTalkList.size() - 1);

                    startSpeak(answer);
                }
            }

            @Override
            public void onError(SpeechError arg0) {

            }
        });

        // 4.显示dialog,接收语音输入
        mDialog.show();
    }

    /**
     * 解析语音json
     * 
     * @param resultString
     * @return
     */
    protected String parseData(String resultString) {
        Gson gson = new Gson();
        VoiceBean voiceBean = gson.fromJson(resultString, VoiceBean.class);

        StringBuffer sb = new StringBuffer();
        ArrayList<WSBean> ws = voiceBean.ws;
        for (WSBean wsBean : ws) {
            String word = wsBean.cw.get(0).w;
            sb.append(word);
        }

        return sb.toString();
    }

    // 语音合成
    public void startSpeak(String text) {
        // 1.创建SpeechSynthesizer对象, 第二个参数:本地合成时传InitListener
        SpeechSynthesizer mTts = SpeechSynthesizer
                .createSynthesizer(this, null);

        // 2.合成参数设置,详见《科大讯飞MSC API手册(Android)》SpeechSynthesizer 类
        // 设置发音人(更多在线发音人,用户可参见 附录12.2
        mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan"); // 设置发音人
        mTts.setParameter(SpeechConstant.SPEED, "50");// 设置语速
        mTts.setParameter(SpeechConstant.VOLUME, "80");// 设置音量,范围0~100
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); // 设置云端
        // mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH,
        // "./sdcard/iflytek.pcm");
        // 3.开始合成
        mTts.startSpeaking(text, null);
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mTalkList.size();
        }

        @Override
        public TalkBean getItem(int position) {
            return mTalkList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(getApplicationContext(),
                        R.layout.list_item, null);
                holder = new ViewHolder();
                holder.tvAsk = (TextView) convertView.findViewById(R.id.tv_ask);
                holder.tvAnswer = (TextView) convertView
                        .findViewById(R.id.tv_answer);
                holder.ivPic = (ImageView) convertView
                        .findViewById(R.id.iv_pic);
                holder.llAnswer = (LinearLayout) convertView
                        .findViewById(R.id.ll_answer);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            TalkBean item = getItem(position);

            if (item.isAsk) {
                holder.tvAsk.setVisibility(View.VISIBLE);
                holder.llAnswer.setVisibility(View.GONE);

                holder.tvAsk.setText(item.text);
            } else {
                holder.tvAsk.setVisibility(View.GONE);
                holder.llAnswer.setVisibility(View.VISIBLE);

                holder.tvAnswer.setText(item.text);

                if (item.imageId > 0) {// 回答内容有图片
                    holder.ivPic.setVisibility(View.VISIBLE);
                    holder.ivPic.setImageResource(item.imageId);
                } else {
                    holder.ivPic.setVisibility(View.GONE);
                }
            }

            return convertView;
        }

    }

    static class ViewHolder {
        public TextView tvAsk;
        public TextView tvAnswer;
        public ImageView ivPic;
        public LinearLayout llAnswer;
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    // 友盟统计
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
MainActivity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lv_list"
        android:layout_width="match_parent"
        android:divider="@null"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bottom_bar"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_selector"
            android:onClick="startRecognize"
            android:text="点击开始语音识别" />
    </LinearLayout>

</LinearLayout>
activity_main
<?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"
    android:padding="5dp" >

    <TextView
        android:id="@+id/tv_ask"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/asker_bubble"
        android:gravity="center"
        android:padding="3dp"
        android:textColor="#000"
        android:textSize="18sp" />

    <LinearLayout
        android:id="@+id/ll_answer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tv_ask"
        android:layout_marginTop="10dp"
        android:background="@drawable/answer_bubble"
        android:orientation="vertical"
        android:padding="3dp" >

        <TextView
            android:id="@+id/tv_answer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="#000"
            android:textSize="18sp" />

        <ImageView
            android:id="@+id/iv_pic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</RelativeLayout>
list_item

 友盟统计http://www.umeng.com/

 二维码、条形码扫描:https://github.com/     搜索 zxing

posted @ 2016-11-16 08:21  ecollab  阅读(315)  评论(0编辑  收藏  举报