uni-app语音转文字功能demo(小程序同声翻译开箱即用)

简介: uni-app语音转文字功能demo(小程序同声翻译开箱即用)



一、同声翻译插件的申请











点击小程序管理后台后,再次点击设置,选中第三方服务



搜索同声传译,将插件添加至自己的小程序服务中

点击详情可看到官方文档及AppId(后续使用中会用到)


二、uni-app中的引用


 

新建项目后,选中manifest.json文件,切换至源码视图(右侧菜单栏最下方!)


在mp--weixin的appid(千万别看错了,上方还有一个AppID)同级下写插件的相关信息

"mp-weixin": {
    /* 小程序特有相关 */
    "appid": "你自己的小程序id",
    "plugins": {
      "WechatSI": {
        "version": "0.3.3",
        "provider": "wx069ba97219f66d99"
      }
    },
    "setting": {
      "urlCheck": false
    },
    "usingComponents": true
  },

特别注意!!!!!!!!!!version版本最好是0.3.3,高于此版本可能会无法使用,并且provider应该都是这个,所以复制即用!

<template>
  <view>
    <!-- 语音识别区 -->
    <!-- 语音内容图片+转文字,没有语音时是隐藏状态 -->
    <view class="content" v-if="shows">
      <!-- 语音声纹图片 -->
      <view class="tet" style="position: relative;">
        <view style="position: absolute;width: 30px;height: 30px;background-color: #e9c2db;right: 0;border-radius: 50%;top: 10px;" :class="num == 1 ? '' : 'op'">
          <!-- 转文字按钮  fanyi为转译事件 -->
          <image style="width: 30px;height: 30px;" src="https://pic.imgdb.cn/item/64c85b431ddac507cc0e7284.png" @tap="fanyi"></image>
        </view>
      </view>
      <!-- 翻译内容点击fanyi后出现 -->
      <view class="voicepad" style="margin-top: 20px;width: 94%;margin-left: 3%;padding: 10px; font-size: 12px;">
        {{endOne}}
      </view>
    </view>
    <!-- 语音音阶动画 长按说话时的动画 -->
    <view class="prompt" v-if="animation">
      <section class="dots-container">
        <view class="dot"></view>
        <view class="dot"></view>
        <view class="dot"></view>
        <view class="dot"></view>
        <view class="dot"></view>
      </section>
      <text>录音中...</text>
    </view>
    <!-- 按钮功能区 -->
    <view class="action" v-show="!shows">
      <!-- 开始录音按钮  长按录音  松手上传 text为-----开始录音----上传录音文字 -->
      <button @longpress="startRecord" @touchend="endRecord" @tap="startRecord">{{text}}</button>
    </view>
    <view class="actioning" v-show="shows">
      <button @tap="playVoice" style="background-color: #d1f2d7;color: #18bc37;">播放录音</button>
      <button @tap="resetVoice" style="background-color: #fdedd9;color: #f3a73f;">重置录音</button>
    </view>
  </view>
</template>
<script>
  // 录音播放事件引入
  const innerAudioContext = uni.createInnerAudioContext();
  innerAudioContext.autoplay = true;
  // 翻译事件引入
  var plugin = requirePlugin("WechatSI")
  let manager = plugin.getRecordRecognitionManager()
  export default {
    data() {
      return {
        text: '开始录音',
        voicePath: '',
        // 音频展示
        shows: false,
        // 动画状态
        animation: false,
        voiceState: "点击录音进行翻译",
        endOne: '',
        num: 1
      }
    },
    onShow() {
    },
    onLoad() {
      // 初始化调用
      this.initRecord();
    },
    methods: {
      startRecord() {
        console.log('开始录音');
        manager.start({
          duration: 30000,
          lang: "zh_CN"
        })
        this.text = '松手上传';
        this.animation = true
      },
      endRecord() {
        console.log('录音结束');
        this.text = '开始录音';
        this.animation = false
        this.shows = true
        manager.stop();
      },
      playVoice() {
        console.log('播放录音');
        if (this.voicePath) {
          innerAudioContext.src = this.voicePath;
          innerAudioContext.play();
        }
      },
      resetVoice() {
        console.log('重置录音');
        innerAudioContext.stop();
        this.shows = false
        this.voicePath = '';
        this.endOne = ''
        this.voiceState = ''
        this.num = 1
      },
      fanyi() {
        if (this.num == 1) {
          console.log(this.voicePath);
          this.endOne = this.voiceState
          this.num = this.num + 1
          uni.showToast({
            title: '转换成功',
            icon: 'success',
            duration: 2000, //持续时间为 2秒
          })
        }else{
          uni.showToast({
            title: '文字已翻译,请勿重复点击',
            icon: 'error',
            duration: 2000, //持续时间为 2秒
          })
        }
      },
      /**
       * 初始化语音识别回调  
       * 绑定语音播放开始事件  
       */
      initRecord: function() {
        manager.onStart = function(res) {
          this.voiceState = "onStart:" + res.msg + "正在录音"
        };
        //有新的识别内容返回,则会调用此事件  
        manager.onRecognize = (res) => {
          this.voiceState = res.result;
        }
        // 识别结束事件  
        manager.onStop = (res) => {
          this.voicePath = res.tempFilePath;
          this.voiceState = res.result;
          if (this.voiceState == '') {
            console.log('没有说话')
            this.endOne = '周围太安静啦~再试试~';
          }
        }
        // 识别错误事件  
        manager.onError = (res) => {
          this.voiceState = '翻译员去吃饭啦,等下再试试';
          this.shows = false
          uni.showToast({
            title: '翻译员去吃饭啦,等下再试试',
            icon: 'error',
            duration: 2000, //持续时间为 2秒
          })
        }
      },
    }
  }
</script>
<style>
  .content {
    box-sizing: border-box;
    width: 98%;
    margin-left: 1%;
    min-height: 300px;
    padding-top: 20px;
    padding-left: 15px;
    padding-right: 15px;
    padding-bottom: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .tet {
    width: 100%;
    height: 50px;
    margin-top: 25px;
    border-radius: 30px;
    background-repeat: no-repeat;
    background-size: 100% 100%;
    background-image: url('https://pic.imgdb.cn/item/64c85a901ddac507cc0d52a4.png');
    position: relative;
  }
  .action {
    position: fixed;
    bottom: 20px;
    width: 100%;
  }
  .action button {
    background-color: #d1f2d7;
    color: #18bc37;
    font-size: 14px;
    display: flex;
    height: 40px;
    width: 96%;
    margin-left: 2%;
    align-items: center;
    justify-content: center;
  }
  .actioning {
    position: fixed;
    bottom: 20px;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .actioning button {
    height: 40px;
    width: 45%;
    border: 0;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .bbig {
    width: 94%;
  }
  .op{
    visibility: hidden;
  }
  /* 动画 */
  .prompt {
    width: 100%;
    height: 80px;
    position: fixed;
    bottom: 70px;
  }
  .prompt text {
    position: absolute;
    bottom: 2px;
    color: #f3a73f;
    left: calc(45%);
    animation: puls 1.5s infinite ease-in-out;
  }
  .dots-container {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 80px;
    width: 45%;
    position: absolute;
    bottom: 0px;
    left: calc(27.5%);
    background-color: #fdedd9;
    border-radius: 20px;
  }
  .dot {
    height: 16px;
    width: 16px;
    margin-right: 10px;
    border-radius: 10px;
    background-color: #f3a73f;
    animation: pulse 1.5s infinite ease-in-out;
  }
  .dot:last-child {
    margin-right: 0;
  }
  .dot:nth-child(1) {
    animation-delay: -0.3s;
  }
  .dot:nth-child(2) {
    animation-delay: -0.1s;
  }
  .dot:nth-child(3) {
    animation-delay: 0.1s;
  }
  @keyframes pulse {
    0% {
      transform: scale(0.8);
      background-color: #f3a73f;
      box-shadow: 0 0 0 0 rgba(243, 167, 63, 0.7);
    }
    50% {
      transform: scale(1.2);
      background-color: #f9d39f;
      box-shadow: 0 0 0 10px rgba(178, 212, 252, 0);
    }
    100% {
      transform: scale(0.8);
      background-color: #f3a73f;
      box-shadow: 0 0 0 0 rgba(243, 167, 63, 0.7);
    }
  }
  @keyframes puls {
    0% {
      transform: translateY(0px)
    }
    50% {
      transform: translateY(-4px)
    }
    100% {
      transform: translateY(0px)
    }
  }
</style>

至此同声翻译就可以使用了,本文主要使用的是录音完毕后,提供播放录音及翻译录音两个选项,也可以改动后支持同声同步翻译。


demo复制即可使用,只需要修改图片,如果真机运行报错,建议切换真机1.0进行测试。

相关实践学习
达摩院智能语音交互 - 声纹识别技术
声纹识别是基于每个发音人的发音器官构造不同,识别当前发音人的身份。按照任务具体分为两种: 声纹辨认:从说话人集合中判别出测试语音所属的说话人,为多选一的问题 声纹确认:判断测试语音是否由目标说话人所说,是二选一的问题(是或者不是) 按照应用具体分为两种: 文本相关:要求使用者重复指定的话语,通常包含与训练信息相同的文本(精度较高,适合当前应用模式) 文本无关:对使用者发音内容和语言没有要求,受信道环境影响比较大,精度不高 本课程主要介绍声纹识别的原型技术、系统架构及应用案例等。 讲师介绍: 郑斯奇,达摩院算法专家,毕业于美国哈佛大学,研究方向包括声纹识别、性别、年龄、语种识别等。致力于推动端侧声纹与个性化技术的研究和大规模应用。
相关文章
|
4天前
|
移动开发 小程序 Android开发
基于 uni-app 开发的废品回收类多端应用功能与界面说明
本文将对一款基于 uni-app 开发的废品回收类多端应用,从多端支持范围、核心功能模块及部分界面展示进行客观说明,相关资源信息也将一并呈现。
32 0
|
9天前
|
存储 小程序 Java
热门小程序源码合集:微信抖音小程序源码支持PHP/Java/uni-app完整项目实践指南
小程序已成为企业获客与开发者创业的重要载体。本文详解PHP、Java、uni-app三大技术栈在电商、工具、服务类小程序中的源码应用,提供从开发到部署的全流程指南,并分享选型避坑与商业化落地策略,助力开发者高效构建稳定可扩展项目。
|
2月前
|
小程序 安全 JavaScript
构建即时通讯APP内的小程序生态体系:从架构设计到技术实现-优雅草卓伊凡
构建即时通讯APP内的小程序生态体系:从架构设计到技术实现-优雅草卓伊凡
132 1
构建即时通讯APP内的小程序生态体系:从架构设计到技术实现-优雅草卓伊凡
|
2月前
|
人工智能 小程序 前端开发
小程序、网站 vs. APP:成本差异究竟在哪里?技术栈如何决定项目上限?优雅草卓伊凡
小程序、网站 vs. APP:成本差异究竟在哪里?技术栈如何决定项目上限?优雅草卓伊凡
207 0
小程序、网站 vs. APP:成本差异究竟在哪里?技术栈如何决定项目上限?优雅草卓伊凡
|
1月前
|
存储 Java PHP
轻量化短视频电商直播带货APP源码全解析:核心功能与设计流程​
在电商直播热潮下,开发专属直播带货APP成为抢占市场关键。本文详解原生开发轻量化APP的核心功能与全流程设计,涵盖用户登录、商品浏览、直播互动、购物车、订单及售后功能,并介绍安卓端Java、苹果端Object-C、后台PHP的技术实现,助力打造高效优质的直播电商平台。
|
3月前
|
数据库
《仿盒马》app开发技术分享-- 回收订单页功能完善(45)
上一节我们实现了订单的待取件、已取消状态展示,并且成功实现了修改订单状态后的列表刷新,实现了云端数据的修改,这一节我们来实现订单页剩下的两个板块的业务逻辑,分别是运输中、已完成状态下的列表展示以及订单状态的修改
90 1
|
2月前
|
小程序 Java 关系型数据库
圈子系统公众号app小程序系统源码圈子系统带即时通讯 多级圈子系统源码 兴趣小组系统开源 私密圈子系统代码 会员制社区系统
本圈子系统解决方案提供即时通讯、多级圈子、兴趣小组、私密社区及会员制管理功能。支持开源与商业方案,推荐ThinkSNS+、EasyClub及OpenFire等系统,并提供前后端技术选型建议,助力快速搭建社交平台。
134 0
|
2月前
|
存储 移动开发 监控
App Trace功能实战:一键拉起、快速安装与免提写邀请码的应用实践
App Trace系统通过一键拉起、快速安装和免提写邀请码三大功能,显著提升用户转化率、安装成功率和邀请注册率。结合深度技术实现与优化,助力公司用户增长,成为移动端核心基础设施。
|
3月前
|
UED
《仿盒马》app开发技术分享-- 扫一扫功能(35)
随着app的逐渐完善,我们现在需要在细节处做更多的打磨,在首页我们添加了很多静态的按钮和组件,现在我们开始对这些组件进行功能的添加,这次首先实现的是首页头部的扫一扫功能,扫一扫我们实现扫码后跳转商品详情页
95 0
|
3月前
|
存储 数据库
《仿盒马》app开发技术分享-- 购物车功能完善(14)
上一节我们实现了购物车商品列表的状态切换,已添加商品数量的增减,已添加商品滑动删除,已添加商品在选中情况下的价格计算。这一节我们在这些功能的基础上实现云端记录,因为我们现在只有数据的查询是从云端获取的,其他的操作虽然都实现了相对应的功能,但是当我们操作完,关闭app,再打开不会有对应的记录,有的同学可能会说,那我们把数据用首选项或者数据库的形式存储就可以了吧? 那如果我更换了另一个设备那这些添加的数据是不是就又不能使用了?所以我们的每个操作,最好都是提交到云端,这样我们在其他设备,在退出应用,切换账号这些情况下都能很好的保存我们操作后的购物车状态。
90 0

热门文章

最新文章