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


    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. }

  • 相关阅读:
    [VIM] config
    【网络协议】聊聊UDP协议
    实现高并发内存池(C++)
    Vellum|SOP —— Vellum Constraints
    mybatis—plus
    如何使用GitHub托管代码(简易版)
    城市天气预报查询易语言代码
    Optional详解
    代码随想录 | Day 60(完结) - LeetCode 84. 柱状图中最大的矩形
    以吉祥物宣传片实力出圈!吉祥物三维动画宣传片怎么制作?
  • 原文地址:https://blog.csdn.net/qq_44890362/article/details/133317678