微信公众号H5,录音功能

news2024/9/21 16:24:45

功能:
按住录音,移开取消,调用的微信录音api,因手机端H5长按图片文字会放大或者选中,得禁止

效果图:
在这里插入图片描述
在这里插入图片描述

html

  <van-popup
      v-model="vanPopupShow"
      round
      position="bottom"
      :style="{ height: '40%' }"
    >
      <div class="bottom_popup">
        <div
          v-if="isTitle"
          class="no-select bottom_popup_p"
          :class="newTitle == '松手取消发送' ? 'isRed' : ''"
          style="height: 20%; width: 100%"
        >
          {{ newTitle }}
        </div>
        <div
          v-else
          class="no-select bottom_popup_p"
          style="height: 20%; width: 100%"
        >
          {{ recordingTime }}s
        </div>
        <!-- <div class="bottom_popup_img">
          <img @touchstart.prevent="startRecording" class="no-select" style="width:50px" src="../../../assets/img/叉.png" alt="">
        </div> -->
        <div
          ref="touchElement"
          :class="{ pressed: isPressed }"
          class="img-select"
          @touchstart.prevent="startRecording"
          @touchend="stopRecording"
          @touchmove="cancelRecording"
        >
          <div v-if="!isTitle" class="siri-like-wave"></div>
        </div>
      </div>
    </van-popup>

js

    startRecording(event) {
      this.isTitle = false;
      this.isPressed = true;
      // 阻止默认的触摸开始事件
      event.preventDefault();
      this.recording = true;
      this.startTime = Date.now();
      this.updateRecordingTime();
      // 获取触摸元素的位置信息
      const rect = this.$refs.touchElement.getBoundingClientRect();
      this.elementBounds = {
        top: rect.top,
        right: rect.right,
        bottom: rect.bottom,
        left: rect.left,
      };

      // 判断初始触摸位置是否在元素内部
      this.isInside = this.isInsideElement(event.touches[0]);
      const _this = this;
      wx.ready(function () {
        wx.startRecord({
          success: () => {
            _this.isrecreding = true;
            if (_this.timer) {
              clearInterval(_this.timer);
              _this.timer = null;
            }
            // 开始录音时启动计时器
            _this.timer = setInterval(() => {
              _this.time++;
            }, 1000);
          },
          fail: (res) => {
            _this.$toast(res);
            console.error(res);
          },
        });
      });
    },
    stopRecording() {
      if (this.newTitle == "松手取消发送") {
        this.recording = false;
        this.isPressed = false;
        // 处理录音结束逻辑
        this.recordingTime = 0;
        this.isTitle = true;
        this.newTitle = "长按开始录音";
        return;
      }
      if (this.recordingTime < 5) {
        this.newTitle = "录音时间太短";
        this.recording = false;
        this.isPressed = false;
        // 处理录音结束逻辑
        this.recordingTime = 0;
        this.isTitle = true;
        return;
      }
      this.recording = false;
      this.isPressed = false;
      // 处理录音结束逻辑
      this.recordingTime = 0;
      this.isTitle = true;
      this.newTitle = "长按开始录音";
      this.vanPopupShow = false;
      wx.stopRecord({
        success: (res) => {
          // this.$toast(res.localId);
          this.localId = res.localId;
          this.uploadRecording();
        },
        fail: (res) => {
          console.error(res);
        },
      });
    },
    uploadRecording() {
      wx.uploadVoice({
        localId: this.localId,
        success: (res) => {
          // this.$toast(res.serverId);
          this.serverId = res.serverId;
          getDownloadVoice({
            mediaId: this.serverId,
          }).then((res) => {
            this.$store.commit("setAudioConfig", {
              fileUrl: res.data,
              isWXAudio: true,
              localId: this.localId,
              // dataBase64Url: 'https://buudoo-tts-demo.oss-cn-beijing.aliyuncs.com/f2b07711-1dcb-4c6e-a94f-0c03d8101674_tongsheng.wav',
            });
            // 停止录音时清除计时器
            clearInterval(this.timer);
            this.timer = null;
            this.time = 0;
            this.$router.push({
              path: "/video/list",
              query: {
                outerTab: 1,
                puuid: this.$route.query.puuid,
              },
            });
          });
        },
        fail: (res) => {
          console.error(res);
        },
      });
    },
    cancelRecording(event) {
      // 判断移动位置是否在元素外部
      // const isOutside = !this.isInsideElement(event.touches[0]);

      // if (this.isInside && isOutside) {
      //   // 在元素内部开始移动,后来移动到外部
      //   // 执行相应的操作
      //   this.isTitle = true;
      //   this.newTitle = "松手取消发送";

      //   // 可以在这里执行你想要的操作
      // } else {
      //   this.isTitle = false;
      //   this.newTitle = "长按开始录音";
      // }

      // const touch = event.touches[0];

      // const deltaY = touch.pageY - touch.clientY;

      // if (deltaY < -this.cancelRecordingThreshold) {
      //   // 取消录音
      //   this.recording = false;
      //   this.recordingTime = 0;
      //   // 处理取消录音逻辑
      // }
    },
    isInsideElement(touch) {
      // 判断触摸点是否在元素内部
      return (
        touch.clientX >= this.elementBounds.left &&
        touch.clientX <= this.elementBounds.right &&
        touch.clientY >= this.elementBounds.top &&
        touch.clientY <= this.elementBounds.bottom
      );
    },
    updateRecordingTime() {
      if (this.recording) {
        const currentTime = Math.floor((Date.now() - this.startTime) / 1000);
        this.recordingTime = currentTime;
        setTimeout(() => {
          this.updateRecordingTime();
        }, 1000);
      }
    },```



css:

```javascript
.siri-like-wave {
  width: 100px;
  height: 100px;
  border: 3px solid #3498db;
  border-top: none;
  border-right: none;
  animation: rotate 2s infinite linear;
  z-index: 1;
  position: absolute;
  top: 0%;
  // left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
}
@keyframes rotate {
  from {
    transform: rotate(0deg);
  }

  to {
    transform: rotate(360deg);
  }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1367257.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ElasticSearch 集群搭建与状态监控cerebro

单机的elasticsearch做数据存储&#xff0c;必然面临两个问题:海量数据存储问题、单点故障问题。为了解决存储能力上上限问题就可以用到集群部署。 海量数据存储问题:将索引库从逻辑上拆分为N个分片(shard)&#xff0c;存储到多个节点单点故障问题:将分片数据在不同节点备份 (r…

SCI一区级 | Matlab实现RIME-CNN-LSTM-Mutilhead-Attention多变量多步时序预测

SCI一区级 | Matlab实现RIME-CNN-LSTM-Mutilhead-Attention多变量多步时序预测 目录 SCI一区级 | Matlab实现RIME-CNN-LSTM-Mutilhead-Attention多变量多步时序预测预测效果基本介绍程序设计参考资料 预测效果 基本介绍 1.Matlab实现RIME-CNN-LSTM-Mutilhead-Attention霜冰算法…

MyBatisPlus学习二:常用注解、条件构造器、自定义sql

常用注解 基本约定 MybatisPlus通过扫描实体类&#xff0c;并基于反射获取实体类信息作为数据库表信息。可以理解为在继承BaseMapper 要指定对应的泛型 public interface UserMapper extends BaseMapper<User> 实体类中&#xff0c;类名驼峰转下划线作为表名、名为id的…

若依CRUD搬砖开始,Java小白入门(十)

背景 经过囫囵吞枣的学习若依框架&#xff0c;对于ruoyi-framework&#xff0c;common&#xff0c;安全&#xff0c;代码生成等模块都看了一圈&#xff0c;剩余的调度模块&#xff0c;这个暂时不深入&#xff0c;剩余的是ruoyi-system&#xff0c;就是用mybatis完成的&#xf…

基于算术优化算法优化的Elman神经网络数据预测 - 附代码

基于算术优化算法优化的Elman神经网络数据预测 - 附代码 文章目录 基于算术优化算法优化的Elman神经网络数据预测 - 附代码1.Elman 神经网络结构2.Elman 神经用络学习过程3.电力负荷预测概述3.1 模型建立 4.基于算术优化优化的Elman网络5.测试结果6.参考文献7.Matlab代码 摘要&…

纯血国产:鸿蒙系统与安卓分道扬镳,对低代码开发行业的影响

近日&#xff0c;科技圈迎来了一则震动性的新闻——鸿蒙系统的“独立宣言”。这一举措意味着鸿蒙系统将与安卓、iOS形成三足鼎立之势&#xff0c;为全球科技市场注入新的活力。 据华为内部人士透露&#xff0c;从明年起&#xff0c;HarmonyOS系统将不再兼容安卓应用&#xff0c…

数模学习day08-拟合算法

这里拟合算法可以和差值算法对比 引入 插值和拟合的区别 与插值问题不同&#xff0c;在拟合问题中不需要曲线一定经过给定的点。拟 合问题的目标是寻求一个函数&#xff08;曲线&#xff09;&#xff0c;使得该曲线在某种准则下与所 有的数据点最为接近&#xff0c;即曲线拟…

springCould中的Hystrix【下】-从小白开始【8】

目录 &#x1f9c2;1.熔断机制❤️❤️❤️ &#x1f32d;2.修改8001服务 ❤️❤️❤️ &#x1f95e;3.测试 ❤️❤️❤️ &#x1f953;4. 服务监控hystrixDashboard❤️❤️❤️ &#x1f32d;5.仪表盘❤️❤️❤️ &#x1f9c2;6.仪表盘的使用 ❤️❤️❤️ 1.熔断机…

Linux的Inode号和日志服务管理

目录 一、Inode号 1.inode和block 2.查看inode信息 二、日志服务管理 1.日志的级别 2.日志的种类 3.日志的功能和日志文件的分类 4.日志的格式和分析工具 三、rsyslog日志处理系统 1、使用Rsyslog创建日志优点 2、Rsyslog配置文件解析 3.通过rsyslog将ssh服务的日志…

k8s---pod基础下

k8s的pod与docker重启策略的区别 k8s的重启策略 always deployment的yaml文件只能是always&#xff0c;pod的yaml三种模式都可以。不论正常退出还是非正常退出都重启。OnFailure&#xff1a;正常退出不重启&#xff0c;非正常退出会重启Never&#xff1a;正常退出和非正常退出…

宿舍安全用电监控系统解决方案

摘 要&#xff1a;很多高校宿舍的用电功率存在限制,为此设计了宿舍用电智能监控系统。系统由主控制器、采集部分、通信部分组成。利用STM32作为主控芯片,采集部分采用BL0937芯片采集电压和电流,采集多条线路用电数据,各路数据采集通道均采用隔离保护,通信部采用4G、LoRa无线通信…

系统架构设计师教程(十)软件可靠性基础知识

软件可靠性基础知识 10.1 软件架构演化和定义的关系10.1.1 演化的重要性10.1.2 演化和定义的关系 10.2 面向对象软件架构演化过程10.2.1 对象演化10.2.2 消息演化10.2.3 复合片段演化10.2.4 约束演化 10.3 软件架构演化方式的分类10.3.1 软件架构演化时期10.3.2 软件架构静态演…

Dockerfile基本结构及编写详解

文章目录 1 Dockerfile1.1 Dockerfile的基本结构1.2 Dockerfile文件说明1.3 Dockerfile常见命令1.4 build命令1.5 部署微服务1.6 docker-compose部署 1 Dockerfile ​ Dockerfile其实就是我们用来构建Docker镜像的源码&#xff0c;当然这不是所谓的编程源码&#xff0c;而是一…

用通俗易懂的方式讲解:2024 检索增强生成技术(RAG)研究进展

本篇内容1w字左右&#xff0c;稍微有点长&#xff0c;相对不容易理解&#xff0c;喜欢可以收藏、关注、点赞。 一、前言 在过去的一两年里&#xff0c;人工智能领域目睹了检索增强生成技术&#xff08;RAG&#xff09;的迅猛发展&#xff0c;这种技术结合了强大的语言模型与信…

百倍潜力股Aleo即将上线,布局正当时!牛市来时,你得有币!

前言 在加密货币市场&#xff0c;2024年被众多市场专家预测为迎来新一轮牛市的关键年份。这一预测背后&#xff0c;潜藏着多种可能推动牛市的因素。其中&#xff0c;下一次比特币&#xff08;BTC&#xff09;的减半事件&#xff0c;以及2024年 BTC 现货ETF的推出&#xff0c;都…

python统计分析——直方图(sns.histplot)

使用seanborn.histplot()函数绘制直方图 from matplotlib.pyplot as plt import seaborn as snsdata_setnp.array([2,3,3,4,4,4,4,5,5,6]) plt.hist(fish_data) &#xff08;1&#xff09;dataNone, 表示数据源。 &#xff08;2&#xff09;xNone, 表示直方图的分布垂直与x轴…

UG装配-爆炸图

当我们将零件装配成总成的时候&#xff0c;通常需要绘制爆炸图来说明总成零件组成&#xff0c;需要用到爆炸图命令&#xff0c;首先点击新建爆炸&#xff0c;然后为爆炸图命名 然后我们可以选择编辑爆炸或者自动爆炸&#xff1a; 编辑爆炸是通过手动的方式选择部件&#xff0c…

岁月匆匆,技术之光

岁月匆匆&#xff0c;技术之光 博主 默语带您 Go to New World. ✍ 个人主页—— 默语 的博客&#x1f466;&#x1f3fb; 《java 面试题大全》 &#x1f369;惟余辈才疏学浅&#xff0c;临摹之作或有不妥之处&#xff0c;还请读者海涵指正。☕&#x1f36d; 《MYSQL从入门到精…

【洛谷学习自留】p9226 糖果

解题思路&#xff1a; 简单的计算题&#xff0c;用n对k取余&#xff0c;如果余数为0&#xff0c;则输出k的值&#xff0c;否则输出&#xff08;k-余数&#xff09;的值。 代码实现&#xff1a; import java.util.Scanner;public class p9226 {public static void main(Strin…

2024年如何借用电商新零售破局?新型商业模式——乐享甄选竞拍模式

2024年如何借用电商新零售破局&#xff1f;新型商业模式——乐享甄选竞拍模式 背景&#xff1a;经历疫情三年的黑天鹅&#xff0c;消费者对未来收入预期和不自信等悲观情绪&#xff0c;从而使得“勒紧腰带&#xff0c;少消费&#xff0c;不消费”&#xff0c;以简单实用成为了新…