• 小程序 语音搜索功能,语音识别翻译成文本进行搜索


    1.使用的是wepy框架

    <view class="{{recordState?'speakInfoActive':'speakInfo'}}" catchtouchstart="touchStart" catchtouchend="touchEnd">按住说话view>

    2.js部分

    1. const plugin = requirePlugin('WechatSI');
    2. const manager = plugin.getRecordRecognitionManager();
    3. export default class send extends wepy.page {
    4. config = {
    5. navigationBarTitleText: '搜索'
    6. };
    7. data = {
    8. recordState: false, //录音状态
    9. }
    10. onLoad(option) {
    11. this.initRecord();
    12. }
    13. }
    14. initRecord(){
    15. manager.stop()
    16. const that = this;
    17. // 有新的识别内容返回,则会调用此事件
    18. manager.onRecognize = function (res) {
    19. console.log(res)
    20. }
    21. manager.onStart = function (res) {
    22. console.log("成功开始录音识别", res)
    23. }
    24. // 识别错误
    25. manager.onError = function (res) {
    26. wx.showToast({
    27. title: `请等待识别完成!`,
    28. icon: 'none',
    29. duration: 2000
    30. })
    31. console.error("error msg", res)
    32. }
    33. //识别结束
    34. manager.onStop = function (res) {
    35. if (res.result == '') {
    36. wx.showModal({
    37. title: '提示',
    38. content: '听不清楚,请重新说一遍!',
    39. showCancel: false,
    40. success: function (res) {}
    41. })
    42. return;
    43. }
    44. that.searchValue = res.result.slice(0, -1)
    45. that.searchFun()
    46. that.$apply()
    47. }
    48. }
    49. //开始
    50. touchStart(e){
    51. const that = this;
    52. that.recordState=true
    53. that.$apply()
    54. manager.start({ lang: 'zh_CN'})
    55. }
    56. //结束
    57. touchEnd(e){
    58. const that = this;
    59. that.recordState=false
    60. that.$apply()
    61. manager.stop();
    62. }
    63. }

    3.样式

    1. .speakInfo{
    2. margin: 1rpx auto;
    3. margin-top: 20rpx;
    4. width: 140rpx;
    5. height: 60rpx;
    6. line-height: 60rpx;
    7. border: 0.5px solid #999;
    8. border-radius: 30rpx;
    9. font-size: 26rpx;
    10. color: #999999;
    11. text-align: center;
    12. }
    13. .speakInfoActive{
    14. margin: 1rpx auto;
    15. margin-top: 20rpx;
    16. width: 140rpx;
    17. height: 60rpx;
    18. line-height: 60rpx;
    19. border: 0.5px solid #3a85fa;
    20. border-radius: 30rpx;
    21. font-size: 26rpx;
    22. color:#ffffff;
    23. text-align: center;
    24. background: #3a85fa;
    25. }

  • 相关阅读:
    【入门-05】存储空间
    Oracle查询语句中做日期加减运算
    2022杭电多校第八场
    【数据结构】栈和队列
    使用pycharm远程连接到Linux服务器进行开发
    Java项目:ssm+mysql+maven养老院管理系统
    5年在职经验之谈:2年功能测试、3年自动化测试,从入门到不可自拔...
    挑战与机遇的交织
    JDBC的基本使用(mysql与java)
    【激光SLAM】基于滤波的激光SLAM方法(Grid-based)
  • 原文地址:https://blog.csdn.net/qq_44890362/article/details/133317678