商城系統(tǒng) 注冊

訊飛語音識別工具類

2020-09-27|HiShop
導讀:package com.example.utils; import com.iflytek.cloud.speech.*; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; /** * Created by songzs on 20...

  package com.example.utils;

  import com.iflytek.cloud.speech.*;

  import java.io.File;

  import java.io.FileInputStream;

  import java.io.IOException;

  import java.util.ArrayList;

  /**

  * Created by songzs on 2017/12/4.

  */

  public class SRTool {

  private int perWaitTime = 100;

  private StringBuffer mResult = new StringBuffer();

  static {

  SpeechUtility.createUtility("appid=********");//申請的appid

  }

  public String voice2words(String fileName) throws InterruptedException, IOException {

  return to(fileName);

  }

  public String to(String fileName) throws InterruptedException, IOException {

  File file = new File(fileName);

  if(!file.exists()){

  throw new RuntimeException("要讀取的文件不存在");

  }

  FileInputStream fis = new FileInputStream(file);

  int len = 0;

  byte[] buf = new byte[fis.available()];

  fis.read(buf);

  fis.close();

  //1.創(chuàng)建SpeechRecognizer對象

  SpeechRecognizer mIat = SpeechRecognizer.createRecognizer();

  //2.設置聽寫參數,詳見《MSC Reference Manual》SpeechConstant類

  mIat.setParameter(SpeechConstant.DOMAIN, "iat");

  mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");

  mIat.setParameter(SpeechConstant.ACCENT, "mandarin ");

  mIat.setParameter(SpeechConstant.AUDIO_SOURCE, "-1");

  //3.開始聽寫

  mIat.startListening(mRecoListener);

  //voiceBuffer為音頻數據流,splitBuffer為自定義分割接口,將其以4.8k字節(jié)分割成數組

  ArrayList buffers = splitBuffer(buf, buf.length, 4800);

  for (int i = 0; i < buffers.size(); i++) {

  // 每次寫入msc數據4.8K,相當150ms錄音數據

  mIat.writeAudio(buffers.get(i), 0, buffers.get(i).length);

  }

  mIat.stopListening();

  while (mIat.isListening()) {

  Thread.sleep(perWaitTime);

  }

  return mResult+"";

  }

  /**

  * 將字節(jié)緩沖區(qū)按照固定大小進行分割成數組

  *

  * @param buffer 緩沖區(qū)

  * @param length 緩沖區(qū)大小

  * @param spsize 切割塊大小

  * @return

  */

  private ArrayList splitBuffer(byte[] buffer, int length, int spsize) {

  ArrayList array = new ArrayList();

  if (spsize <= 0 || length <= 0 || buffer == null

  || buffer.length < length)

  return array;

  int size = 0;

  while (size < length) {

  int left = length - size;

  if (spsize < left) {

  byte[] sdata = new byte[spsize];

  System.arraycopy(buffer, size, sdata, 0, spsize);

  array.add(sdata);

  size += spsize;

  } else {

  byte[] sdata = new byte[left];

  System.arraycopy(buffer, size, sdata, 0, left);

  array.add(sdata);

  size += left;

  }

  }

  return array;

  }

  //聽寫監(jiān)聽器

  private RecognizerListener mRecoListener = new RecognizerListener() {

  public void onResult(RecognizerResult results, boolean isLast) {

  System.out.println("Result:" + results.getResultString());

  mResult.append(results.getResultString());

  }

  //會話發(fā)生錯誤回調接口

  public void onError(SpeechError error) {

  System.out.println(error.getErrorCode()+"=========="+error.getErrorDesc());

  System.out.println(error);

  }

  //開始錄音

  public void onBeginOfSpeech() {

  }

  //音量值0~30

  public void onVolumeChange(int volume) {

  }

  @Override

  public void onVolumeChanged(int i) {

  }

  @Override

  public void onEndOfSpeech() {

  }

  @Override

  public void onEvent(int i, int i1, int i2, String s) {

  }

  };

  }

  最后提醒大家,小程序上傳接口必須是https請求,所以可能需要搭建https,

HiShop小程序工具提供多類型商城/門店小程序制作,可視化編輯 1秒生成5步上線。通過拖拽、拼接模塊布局小程序商城頁面,所看即所得,只需要美工就能做出精美商城。

更多小程序開發(fā)案例,盡在:http://m.descansotropical.com/xiaocx/kaifa.html 

電話咨詢 預約演示 0元開店