AndroidStudio快速开发讯飞语音听写

来源:互联网 发布:java c3p0连接池使用 编辑:程序博客网 时间:2024/05/19 13:23

  好久没更新了,差点就忘记这个东东了,不过好在一闲下来就赶紧更新一波 哈哈^_^
**此博客主要针对将要进行语音开发的小白,如有不对的地方,请指正

到科大讯飞官网注册账号并登陆,选择单个服务SDK下载 ,选择语音听写服务、Android平台并新建一个应用,下载完成后解压。如下图
这里写图片描述

1.新建一个项目,将解压后的assets文件复制在项目的main目录下,并且在main目录下新建一个名为jniLibs的文件夹,将libs中的所有文件夹复制到jniLibs中,然后把libs的Msc.jar和Sunflower.jar放在项目的libs下并且右键选择添加到库。项目的文件目录如下
这里写图片描述
2.将打开sample–SpeechDemo–src–com–iflytek–speech-util,将FucUtil.java(功能性函数扩展类)和JsonParser.java(Json结果解析类)复制到utils包下

3.这里我直接将代码放过来,有不懂得可以看下注释

public class RecognitionActivity extends AppCompatActivity {    //显示听写结果    private TextView textView;    //语音听写对象    private SpeechRecognizer speechRecognizer;    //语音听写UI    private RecognizerDialog recognizerDialog;    //是否显示听写UI    private boolean isShowDialog = true;    //缓存    private SharedPreferences sharedPreferences;    //用hashmap存储听写结果    private HashMap<String, String> hashMap = new LinkedHashMap<String, String>();    //引擎类型(云端或本地)    private String mEngineType = null;    //函数返回值    private int ret = 0;    private Toast toast;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_recognition);        initView();        initDate();    }    private void initDate() {        //初始化sdk 将自己申请的appid放到下面        //此句代码应该放在application中的,这里为了方便就直接放代码中了        SpeechUtility.createUtility(this, "appid=你自己申请的appid");        speechRecognizer = SpeechRecognizer.createRecognizer(this, initListener);        recognizerDialog = new RecognizerDialog(this, initListener);        sharedPreferences = getSharedPreferences(this.getPackageName(), Context.MODE_PRIVATE);        toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);        //这里我直接将引擎类型设置为云端,因为本地需要下载讯飞语记,这里为了方便直接使用云端        //有需要的朋友可以加个单选框 让用户选择云端或本地        mEngineType = SpeechConstant.TYPE_CLOUD;    }    private void initView() {        textView = (TextView) findViewById(R.id.tv);    }    //开始听写    public void start(View view) {        textView.setText("");        hashMap.clear();        setParams();        if (isShowDialog) {            recognizerDialog.setListener(dialogListener);            recognizerDialog.show();        } else {            ret = speechRecognizer.startListening(recognizerListener);            if (ret != ErrorCode.SUCCESS) {                Log.e("tag", "听写失败,错误码" + ret);            }        }    }    //结束听写    public void stop(View view) {        Toast.makeText(this, "停止听写", Toast.LENGTH_SHORT).show();        if (isShowDialog) {            recognizerDialog.dismiss();        } else {            speechRecognizer.stopListening();        }    }    //初始化监听器    private InitListener initListener = new InitListener() {        @Override        public void onInit(int i) {            if (i != ErrorCode.SUCCESS) {                Log.e("tag", "初始化失败,错误码" + i);            }        }    };    //无UI监听器    private RecognizerListener recognizerListener = new RecognizerListener() {        @Override        public void onVolumeChanged(final int i, byte[] bytes) {            Log.e("tag", "返回数据大小" + bytes.length);            runOnUiThread(new Runnable() {                @Override                public void run() {                    toast.setText("当前音量" + i);                }            });        }        @Override        public void onBeginOfSpeech() {            Log.e("tag", "开始说话");        }        @Override        public void onEndOfSpeech() {            Log.e("tag", "结束说话");        }        @Override        public void onResult(RecognizerResult recognizerResult, boolean b) {            if (recognizerResult != null) {                Log.e("tag", "听写结果:" + recognizerResult.getResultString());                printResult(recognizerResult);            }        }        @Override        public void onError(SpeechError speechError) {            Log.e("tag", "错误信息" + speechError.getPlainDescription(true));        }        @Override        public void onEvent(int i, int i1, int i2, Bundle bundle) {            // 以下代码用于获取与云端的会话id,当业务出错时将会话id提供给技术支持人员,可用于查询会话日志,定位出错原因            // 若使用本地能力,会话id为null            //  if (SpeechEvent.EVENT_SESSION_ID == eventType) {            //      String sid = obj.getString(SpeechEvent.KEY_EVENT_SESSION_ID);            //      Log.d(TAG, "session id =" + sid);            //  }        }    };    //有UI监听器    private RecognizerDialogListener dialogListener = new RecognizerDialogListener() {        @Override        public void onResult(RecognizerResult recognizerResult, boolean b) {            if (recognizerResult != null) {                Log.e("tag", "听写结果:" + recognizerResult.getResultString());                printResult(recognizerResult);            }        }        @Override        public void onError(SpeechError speechError) {            Log.e("tag", speechError.getPlainDescription(true));        }    };    //输出结果,将返回的json字段解析并在textVie中显示    private void printResult(RecognizerResult results) {        String text = JsonParser.parseIatResult(results.getResultString());        String sn = null;        // 读取json结果中的sn字段        try {            JSONObject resultJson = new JSONObject(results.getResultString());            sn = resultJson.optString("sn");        } catch (JSONException e) {            e.printStackTrace();        }        hashMap.put(sn, text);        StringBuffer resultBuffer = new StringBuffer();        for (String key : hashMap.keySet()) {            resultBuffer.append(hashMap.get(key));        }        textView.setText(resultBuffer.toString());    }    private void setParams() {        //清空参数        speechRecognizer.setParameter(SpeechConstant.PARAMS, null);        //设置引擎        speechRecognizer.setParameter(SpeechConstant.ENGINE_TYPE, mEngineType);        //设置返回数据类型        speechRecognizer.setParameter(SpeechConstant.RESULT_TYPE, "json");        //设置中文 普通话        speechRecognizer.setParameter(SpeechConstant.LANGUAGE, "zh_cn");        speechRecognizer.setParameter(SpeechConstant.ACCENT, "mandarin");        // 设置语音前端点:静音超时时间,即用户多长时间不说话则当做超时处理        speechRecognizer.setParameter(SpeechConstant.VAD_BOS,                sharedPreferences.getString("iat_vadbos_preference", "4000"));        // 设置语音后端点:后端点静音检测时间,即用户停止说话多长时间内即认为不再输入, 自动停止录音        speechRecognizer.setParameter(SpeechConstant.VAD_EOS,                sharedPreferences.getString("iat_vadeos_preference", "1000"));        // 设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点        speechRecognizer.setParameter(SpeechConstant.ASR_PTT,                sharedPreferences.getString("iat_punc_preference", "0"));        // 设置音频保存路径,保存音频格式支持pcm、wav,设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限        // 注:AUDIO_FORMAT参数语记需要更新版本才能生效        speechRecognizer.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");        speechRecognizer.setParameter(SpeechConstant.ASR_AUDIO_PATH,                Environment.getExternalStorageDirectory() + "/msc/iat.wav");    }}

布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/activity_recognition"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <TextView        android:id="@+id/tv"        android:layout_width="match_parent"        android:layout_height="100dp"        android:background="#55cccccc" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:onClick="start"            android:text="开始听写" />        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:onClick="stop"            android:text="停止听写" />    </LinearLayout></LinearLayout>

注释:appid可以在登陆科大讯飞后进入控制台中查看,项目中的注释有我个人的想法,所以可能和官方的不太一样,大家以理解为主

还有别忘了在AndroidManifest.xml中添加权限

    <!-- 讯飞语音权限开始 -->    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />    <uses-permission android:name="android.permission.RECORD_AUDIO" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <!-- 讯飞语音权限结束 -->

到此,讯飞平台的语音听写功能就实现了,大家赶紧来试一下吧!

0 0
原创粉丝点击