当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序语音同步智能识别的实现案例代码解析

微信小程序语音同步智能识别的实现案例代码解析

2020年06月23日  | 移动技术网IT编程  | 我要评论

一、背景

在小程序的一些应用场景中,会有语音转文字的需求。原有的做法一般是先通过小程序的录音功能录下语音文件,然后再通过调用语音智能识别webapi(比如百度云ai平台,科大讯飞平台)将语音文件转成文字信息,以上的做法比较繁琐且用户的体验性较差。

为解决此问题,微信直接开放了同声传译的插件,小程序作者可以直接使用该插件进行语音同声传译的开发。此文章将通过前后端整合应用的完整案例完成语音的实时转换,并将语音上传到服务端后台备份。

二、同声传译插件介绍

微信同声传译由微信智聆语音团队、微信翻译团队与公众平台联合推出的同传开放接口,首期开放语音转文字、文本翻译、语音合成接口,为开发者赋能。

1、 微信小程序后台添加插件

进入微信小程序后台-->进入设置-->第三方设置-->添加插件->搜索同声传译-->完成添加。


2、 微信小程序启用插件

在小程序app.json文件中增加插件版本等信息:

"plugins": {
 "wechatsi": {
 "version": "0.3.3",
 "provider": "wx069ba97219f66d99"
 }
 },

在页面程序文件中引入插件:

/* index.js */

const plugin = requireplugin("wechatsi")

// 获取**全局唯一**的语音识别管理器**recordrecomanager**
const manager = plugin.getrecordrecognitionmanager()

recordrecomanager 对象的方法列表:

方法 参数 说明
start options 开始识别
stop 结束识别
onstart callback 正常开始录音识别时会调用此事件
onrecognize callback 有新的识别内容返回,则会调用此事件
onstop callback 识别结束事件
onerror callback 识别错误事件

官方开发文档:插件的语音识别管理器

三、语音同步转换的前端实现

1、界面ui与操作

ui参考微信官方的demo:长按按钮进行录音,松开按钮实时将录音转换为文字。

用户可对同步转换的文字进行编辑,同时可将原始语音文件与文字上传后台服务端。

2、代码实现

语音同步转换的主要代码:

//导入插件
const plugin = requireplugin("wechatsi");
// 获取**全局唯一**的语音识别管理器**recordrecomanager**
const manager = plugin.getrecordrecognitionmanager();

/**
 * 加载进行初始化
 */
 onload: function () {
 	//获取录音权限
	app.getrecordauth();
	//初始化语音识别回调
 this.initrecord();
 },

 ...
 
/**
 * 初始化语音识别回调
 * 绑定语音播放开始事件
 */
 initrecord: function () {
 //有新的识别内容返回,则会调用此事件
 manager.onrecognize = (res) => {
 let currentdata = object.assign({}, this.data.currenttranslate, {
 text: res.result,
 });
 this.setdata({
 currenttranslate: currentdata,
 });
 this.scrolltonew();
 };

 // 识别结束事件
 manager.onstop = (res) => {
 let text = res.result;

 console.log(res.tempfilepath);

 if (text == "") {
 this.showrecordemptytip();
 return;
 }

 let lastid = this.data.lastid + 1;

 let currentdata = object.assign({}, this.data.currenttranslate, {
 text: res.result,
 translatetext: "正在识别中",
 id: lastid,
 voicepath: res.tempfilepath,
 duration: res.duration
 });

 this.setdata({
 currenttranslate: currentdata,
 recordstatus: 1,
 lastid: lastid,
 });
 //将当前识别内容与语音文件加入列表
 this.addrecordfile(currentdata, this.data.dialoglist.length);
 //刷新列表
	 this.scrolltonew();
 };

 // 识别错误事件
 manager.onerror = (res) => {
 this.setdata({
 recording: false,
 bottombuttondisabled: false,
 });
 };

 },

 /**
 * 按住按钮开始语音识别
 */
 streamrecord: function (e) {
 let detail = e.detail || {};
 let buttonitem = detail.buttonitem || {};
 //开始中文录音
 manager.start({
 lang: buttonitem.lang,
 });

 this.setdata({
 recordstatus: 0,
 recording: true,
 currenttranslate: {
 // 当前语音输入内容
 create: util.recordtime(new date()),
 text: "正在聆听中",
 lfrom: buttonitem.lang,
 lto: buttonitem.lto,
 },
 });
 //刷新列表
 this.scrolltonew();
 },

 /**
 * 松开按钮结束语音识别
 */
 streamrecordend: function (e) {
 let detail = e.detail || {}; // 自定义组件触发事件时提供的detail对象
 let buttonitem = detail.buttonitem || {};

 // 防止重复触发stop函数
 if (!this.data.recording || this.data.recordstatus != 0) {
 console.warn("has finished!");
 return;
 }

 manager.stop();

 this.setdata({
 bottombuttondisabled: true,
 });
 },

编辑识别文字并完上传的主要代码:

/**
 * 页面的初始数据
 */
 data: {
 edit_text_max: 200,
 remain_length: 200,
 edit_text: "",
 is_focus: false,
 tips: "",
 index: -1,
 voicepath: "",
 
 },

/**
 * 加载初始化
 */
 onload: function (options) {
 //根据传入的文字内容填充编辑框
 this.setedittext(options.content)
 
 this.setdata({
 index: index,
 oldtext:options.content,
 voicepath: options.voicepath
 })
 
 },

 /**
 * 编辑文字
 */
 editinput: function (event) {
 console.log(event)
 if (event.detail.value.length > this.getedittextmax()) {

 } else {
 this.data.edit_text = event.detail.value
 this.updateremainlength(this.data.edit_text)
 }
 },

 /**
 * 上传文字与语音文件
 */
 editconfirm: function (event) {
 let json=this.data.edit_text
 //调用微信上传文件api将信息上传至服务端webapi
 wx.uploadfile({
 url: api.wxfileuploadurl,
 filepath: this.data.voicepath,
 name: "file",
 header: {
 authorization: wx.getstoragesync("loginflag"),
 "content-type": "multipart/form-data",
 },
 formdata: {
 openid: app.globaldata.userinfo.openid,
 realname: "语音文件",
 json: json.stringify(json),
 },
 success: (result) => {
 console.log("success:", result);
 if (result.statuscode == "200") {
  let data = json.parse(result.data);
  console.log("data", data);
  if (data.success == true) {
  let module = data.module;
  console.log("module", module);
  app.showinfo("上传成功");  
  settimeout( ()=>{
  wx.navigateback();
  }, 2000)
   
  } else {
  app.showinfo("异常错误" + data.errmsg + ",请重新进入");
  wx.navigateto({
  url: "/pages/index/index",
  });
  }
 } else {
  app.showinfo("访问后台异常,重新进入系统");
  wx.navigateto({
  url: "/pages/index/index",
  });
 }
 },
 fail: (result) => {
 console.log("fail", result);
 wx.navigateto({
  url: "/pages/index/index",
 });
 },
 complete: () => {},
 });

 },

四、后端springboot实现语音文件上传webapi

1、springboot项目api相关结构树

2、文件上传工具类的实现

tools工具类包中主要存文件通用的文件上传工具类,该工具类会将文件上传至配置指定的文件夹下,并将文件信息写入upload_file表中。

  • 文件信息实体类:与数据库中表upload_file对应;
  • 文件存储仓库类:通过spring data jpa接口实现数据的crud;
  • 文件上传工具接口:对外统一封装文件上传方法;
  • 文件上传工具实现类:实现文件上传方法接口。

文件信息实体类:uploadfile.java

/**
 * 文件信息表
 *
 * @author zhuhuix
 * @date 2020-04-20
 */
@entity
@getter
@setter
@table(name = "upload_file")
public class uploadfile {

 @id
 @generatedvalue(strategy = generationtype.identity)
 @notnull(groups = update.class)
 private long id;

 /**
 * 文件实际名称
 */
 @column(name = "real_name")
 private string realname;

 /**
 * 文件名
 */
 @notnull
 @column(name = "file_name")
 private string filename;

 /**
 * 文件主名称
 */
 @notnull
 @column(name = "primary_name")
 private string primaryname;

 /**
 * 文件扩展名
 */
 @notnull
 private string extension;

 /**
 * 存放路径
 */
 @notnull
 private string path;

 /**
 * 文件类型
 */
 private string type;

 /**
 * 文件大小
 */
 private long size;

 /**
 * 上传人
 */
 private string uploader;

 @jsonignore
 @column(name = "create_time")
 @creationtimestamp
 private timestamp createtime;

 public uploadfile(string realname, @notnull string filename, @notnull string primaryname, @notnull string extension, @notnull string path, string type, long size, string uploader) {
 this.realname = realname;
 this.filename = filename;
 this.primaryname = primaryname;
 this.extension = extension;
 this.path = path;
 this.type = type;
 this.size = size;
 this.uploader = uploader;
 }

 @override
 public string tostring() {
 return "uploadfile{" +
  "filename='" + filename + '\'' +
  ", uploader='" + uploader + '\'' +
  ", createtime=" + createtime +
  '}';
 }
}

文件存储仓库类:uploadfilerepository.java

/**
 * 上传文件dao接口层
 *
 * @author zhuhuix
 * @date 2020-04-03
 */
public interface uploadfilerepository extends jparepository<uploadfile, long>, jpaspecificationexecutor<uploadfile> {
//该接口继承jparepository及crudrepository接口,已实现了如findbyid,save,delete等crud方法
}

uploadfilerepository 接口继承jparepository及crudrepository接口,已实现了如findbyid,save,delete等crud方法

文件上传工具接口:uploadfiletool.java

/**
 * 文件上传接口定义
 *
 * @author zhuhuix
 * @date 2020-04-20
 */
public interface uploadfiletool {

 /**
 * 文件上传
 * @param multipartfile 文件
 * @return 上传信息
 */
 uploadfile upload(string uploader,string realname,multipartfile multipartfile);
}

文件上传工具实现类:uploadfiletoolimpl.java

/**
 * 文件上传实现类
 *
 * @author zhuhuix
 * @date 2020-04-20
 */
@service
@transactional(propagation = propagation.supports, readonly = true, rollbackfor = exception.class)
public class uploadfiletoolimpl implements uploadfiletool {

 private final uploadfilerepository uploadfilerepository;

 @value("${uploadfile.path}")
 private string path;

 @value("${uploadfile.maxsize}")
 private long maxsize;

 public uploadfiletoolimpl(uploadfilerepository uploadfilerepository) {
 this.uploadfilerepository = uploadfilerepository;
 }

 @override
 @transactional(rollbackfor = exception.class)
 public uploadfile upload(string uploader, string realname, multipartfile multipartfile) {
 //检查文件大小
 if (multipartfile.getsize() > maxsize * constant.mb) {
  throw new runtimeexception("超出文件上传大小限制" + maxsize + "mb");
 }
 //获取上传文件的主文件名与扩展名
 string primaryname = fileutil.mainname(multipartfile.getoriginalfilename());
 string extension = fileutil.extname(multipartfile.getoriginalfilename());
 //根据文件扩展名得到文件类型
 string type = getfiletype(extension);
 //给上传的文件加上时间戳
 localdatetime date = localdatetime.now();
 datetimeformatter format = datetimeformatter.ofpattern("yyyymmddhhmmsss");
 string nowstr = "-" + date.format(format);
 string filename = primaryname + nowstr + "." + extension;

 try {
  string filepath = path + type + file.separator + filename;
  file dest = new file(filepath).getcanonicalfile();
  if (!dest.getparentfile().exists()) {
  dest.getparentfile().mkdirs();
  }
  multipartfile.transferto(dest);
  if (objectutil.isnull(dest)) {
  throw new runtimeexception("上传文件失败");
  }

  uploadfile uploadfile = new uploadfile(realname, filename, primaryname, extension, dest.getpath(), type, multipartfile.getsize(), uploader);
  return uploadfilerepository.save(uploadfile);

 } catch (exception e) {
  e.printstacktrace();
  throw new runtimeexception(e.getmessage());
 }

 }

 /**
 * 根据文件扩展名给文件类型
 *
 * @param extension 文件扩展名
 * @return 文件类型
 */
 private static string getfiletype(string extension) {
 string document = "txt doc pdf ppt pps xlsx xls docx csv";
 string music = "mp3 wav wma mpa ram ra aac aif m4a";
 string video = "avi mpg mpe mpeg asf wmv mov qt rm mp4 flv m4v webm ogv ogg";
 string image = "bmp dib pcp dif wmf gif jpg tif eps psd cdr iff tga pcd mpt png jpeg";
 if (image.contains(extension)) {
  return "image";
 } else if (document.contains(extension)) {
  return "document";
 } else if (music.contains(extension)) {
  return "music";
 } else if (video.contains(extension)) {
  return "video";
 } else {
  return "other";
 }
 }
}

注意,该程序代码中用到了@value注解获取配置文件中的uploadfile.path及uploadfile.maxsize参数,一般在项目静态配置文件中按如下书写(yml配置文件)。

# 测试环境文件存储路径
uploadfile:
 path: c:\startup\file\
 # 文件大小 /m
 maxsize: 50

3、小程序上传文件接口的实现

wx-miniprogram包定义了小程序crm webapi的接口,小程序调用webapi实现文件的上传及其他功能。

  • 微信小程序 webapi:对外提供小程序上传文件webapi;
  • 微信小程序服务接口:封装小程序上传文件服务接口;
  • 微信小程序服务实现:小程序上传文件服务的实现,该服务实现中会调用tools包中的uploadfile接口进行文件的上传。

微信小程序crm webapi:wxminicrmcontroller.java

/**
 * 微信小程序crm webapi
 *
 * @author zhuhuix
 * @date 2020-03-30
 */
@slf4j
@restcontroller
@requestmapping("/api/wx-mini")
@api(tags = "微信小程序crm接口")
public class wxminicrmcontroller {

 private final wxminicrm wxminicrm;

 public wxminicrmcontroller(wxminicrm wxminicrm) {
 this.wxminicrm = wxminicrm;
 }

 @apioperation(value = "微信小程序端上传文件")
 @postmapping(value = "/fileupload")
 public responseentity fileupload(httpservletrequest request) {
 multiparthttpservletrequest req = (multiparthttpservletrequest) request;

 multipartfile multipartfile = req.getfile("file");
 string openid = req.getparameter("openid");
 string realname = req.getparameter("realname");
 string json = req.getparameter("json");

 return responseentity.ok(wxminicrm.uploadfile(json, openid,realname, multipartfile));

 }
}

微信小程序crm服务接口:wxminicrm.java

/**
 * 微信小程序crm服务接口定义
 *
 * @author zhuhuix
 * @date 2020-04-20
 */
public interface wxminicrm {

 /**
 * 将微信小程序传入的json对象写入数据库,并同时将文件上传至服务端
 *
 * @param json  微信端传入json对象
 * @param openid 上传人
 * @param realname 文件实际名称
 * @param multipartfile 上传文件
 * @return 返回上传信息
 */
 result<uploadfile> uploadfile(string json, string openid, string realname,multipartfile multipartfile);
}

微信小程序crm服务实现:wxminicrmimpl.java

/**
 * 微信小程序crm实现类
 *
 * @author zhuhuix
 * @date 2020-04-20
 */
@slf4j
@service
@transactional(propagation = propagation.supports, readonly = true, rollbackfor = exception.class)
public class wxminicrmimpl implements wxminicrm {

 private final uploadfiletool uploadfiletool;

 public wxminicrmimpl(uploadfiletool uploadfiletool) {
 this.uploadfiletool = uploadfiletool;
 }

 @override
 @transactional(rollbackfor = exception.class)
 public result<uploadfile> uploadfile(string json, string openid,string realname, multipartfile multipartfile) {
 return new result<uploadfile>().ok(uploadfiletool.upload(openid,realname, multipartfile));
 }
}

4、小程序上传文件接口的查看

访问swagger2可查看该接口,swagger2与springboot的集成可参考springboot jwt认证机制项目集成swagger2

五、实际测试

语音测试正常

上传文件至后台:

上传的日志信息查看:

总结

到此这篇关于微信小程序语音同步智能识别的实现案例代码解析的文章就介绍到这了,更多相关微信小程序语音同步智能识别内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网