您当前的位置: 首页 > 知识百科 > 微信小程序语音格式转换怎么弄?

微信小程序语音格式转换怎么弄?

时间:2023-07-01 14:05 阅读数:129 人阅读 分类:知识百科

  微信小程序语音功能的实现是需要首先将语音转换成小程序允许的格式,所以很多人不了解微信小程序语音格式转换要怎么操作,小编今天会为大家具体讲解一下,希望能够帮助到大家。

  微信小程序的语音格式是silk,所以我们要将语音转换成silk格式。步骤和小程序代码如下:

  1、上传silk文件

  2、下载silk-v3-decoder,通过名称把silk转换成讯飞可识别的wav文件

  3、获取讯飞转换后的小程序文字信息

  [java] view plain copy/**

  * 获取wav文件 并返回路径

  * @param wavFileName

  * @return

  * @throws Exception

  */

  public String getWav(String wavFileName) throws Exception {//之前别人写的代码,可以改为java直接执行shell脚本的方式

  StringBuilder command = new StringBuilder(Constant.FFMPEG);

  command.append(Constant.CONVERTER);

  command.append(Constant.OXM_VOICE_UPLOAD_BASE_PTAH);

  command.append(wavFileName);

  command.append(" wav ");

  Session session = jschSessionService.getSession();

  jschSessionService.getChannelAndExec(session, command.toString());

  Thread.sleep(2000);

  String name = wavFileName.replace("silk", "wav");

  return name;

  }

  public static final String FFMPEG = " export PATH=/usr/local/ffmpeg/bin/:$PATH; source /etc/profile;";

  public static final String CONVERTER = " sh /home/deployer/silk-v3-decoder-master/converter.sh ";

  public static final String OXM_VOICE_UPLOAD_BASE_PTAH = "/home/deployer/tomcat7/webapps/xxxx/";

  [java] view plain copyimport java.io.BufferedReader;

  import java.io.IOException;

  import java.io.InputStream;

  import java.io.InputStreamReader;

  import java.nio.charset.Charset;

  import java.util.Properties;

  import org.springframework.stereotype.Service;

  import com.jcraft.jsch.Channel;

  import com.jcraft.jsch.ChannelExec;

  import com.jcraft.jsch.JSch;

  import com.jcraft.jsch.JSchException;

  import com.jcraft.jsch.Session;

  import com.shanjin.oxmmon.util.Constant;

  /**

  * java ssh 服务

  *

  */

  @Service("JschSessionService")

  public class JschSessionService {

  public Session getSession() throws JSchException {

  Properties sshConfig = new Properties();

  sshConfig.put("StrictHostKeyChecking", "no");

  JSch jsch = new JSch();

  Session session = jsch.getSession(Constant.SSH_USER,Constant.SSH_IP,Constant.SSH_PORT);

  session.setPassword(Constant.SSH_PASSWORD);

  session.setConfig(sshConfig);

  session.connect();

  return session;

  }

  public void closeSession(Session session) {

  if (session.isConnected()) {

  session.disconnect();

  }

  }

  public String getChannelAndExec(Session session, String command) throws JSchException, IOException {

  Channel channel = session.openChannel("exec");

  ((ChannelExec) channel).setCommand(command);

  channel.setInputStream(null);

  ((ChannelExec) channel).setErrStream(System.err);

  channel.connect();

  int state = channel.getExitStatus();

  /*if (state != 0) {

  return "exec is error ,exit code is :" + state;

  }*/

  ((ChannelExec) channel).setErrStream(System.err);

  //String msg = getExitMessage(channel);

  closeChannel(channel);

  closeSession(session);

  return state + "";

  }

  public void closeChannel(Channel channel){

  if(channel.isConnected()){

  channel.disconnect();

  }

  }

  public String getExitMessage(Channel channel) throws IOException {

  StringBuilder message = new StringBuilder("exit message is : ");

  InputStream in = channel.getInputStream();

  BufferedReader reader = new BufferedReader(new InputStreamReader(in,

  Charset.forName("utf-8")));

  String buf = null;

  while ((buf = reader.readLine()) != null) {

  message.append(buf);

  }

  return message.toString();

  }

  }

  [java] view plain copy /**

  * 获取语音信息

  */

  public String getVoiceText(String filePath,String wavName) throws Exception {

  IflyTekServiceImpl iflytekService = new IflyTekServiceImpl();//TODO 注解方式,会产生缓存,一直都是第一次识别的语音

  File ff = null;

  String text = null;

  File file = new File(filePath+wavName);

  InputStream in = new FileInputStream(file);

  byte[] bb = VoiceSplitUtil.getbytes(in);

  System.out.println(bb);

  text = iflytekService.RecognizePcmfileByte(bb);

  // text = this.getText(fly);

  in.close();

  return text;

  }

  public String getText(IflyTekServiceImpl fly) {

  StringBuilder text = new StringBuilder();

  JSONObject json = null;

  JSONArray wss = null;

  JSONObject ws = null;

  JSONArray cw = null;

  JSONObject c = null;

  long begin = System.currentTimeMillis();

  while (true) {

  long end = System.currentTimeMillis();

  if((end - begin) >3000){

  break;

  }

  if (fly.recognizer.isListening() && fly.mIsEndOfSpeech == true) {

  System.out.println(fly.sn);

  json = JSONObject.fromObject(fly.sn.toString());

  wss = json.getJSONArray("ws");

  text = new StringBuilder();

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

  ws = wss.getJSONObject(i);

  cw = ws.getJSONArray("cw");

  c = cw.getJSONObject(0);

  text.append(c.getString("w"));

  }

  break;

  }

  }

  if(text.length() <= 0){

  text.append("对不起,我没听清您在说什么");

  }

  return text.toString();

  }

  按照上文中提供的代码,你就可以进行微信小程序语音格式转换的开发了,是不是很简单呢?开发成功之后你会发现小程序语音功能其实还蛮有用的,快试试吧!想要获取更多资料请关注微信小程序商店。

  

  小程序语音录入框怎样使用?

  微信小程序如何实现语音输入搜索?

  怎样才能让微信小程序语音转文字