微信小程序 长按录音+录制视频

news/2024/7/20 2:32:17 标签: 微信小程序, 音视频, 小程序

<view class="bigCircle" bindtouchstart="start" bindtouchend="stop">
  <view class="smallCircle {{startVedio?'onVedio':''}}">
    <text>{{startVedio?'正在录音':'长按录音'}}</text>
  </view>
</view>
<view>
  <view class="vedio-player">
    <view class="vedio-file" bindtap="play">
      <view class="item-list">
        <view wx:for="{{20}}" wx:for-item="item" class="item {{vedioProcess>item*5?'active':''}}" wx:key="index"></view>
      </view>
      <view class="time">{{vedioTime}}</view>
    </view>

  </view>
</view>


<view class="page-body" style="margin-top: 100rpx;">
  <view class="page-body-wrapper">
    <camera device-position="back" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>
    <view class="btn-area">
      <button type="primary" bindtap="startRecord">开始录像</button>
    </view>
    <view class="btn-area">
      <button type="primary" bindtap="stopRecord">结束录像</button>
    </view>
    <view class="preview-tips">预览</view>
    <video wx:if="{{videoSrc}}" class="video" src="{{videoSrc}}"></video>
  </view>
</view>
.bigCircle{height:140rpx;width:140rpx;border-radius:100%;background-color:#3370FF2E;display:flex;align-items:center;justify-content:center;color:#fff}
.smallCircle{width:100rpx;height:100rpx;background-color:#3370ff;border-radius:100%;display:flex;align-items:center;justify-content:center; font-size: 20rpx;}
.onVedio{background-color:#04d7b9!important}
.vedio-player{margin-top:20px;display:flex;justify-content:center;align-items:center}
.vedio-file{width:250px;height:44px;background:#f5f5f5;border-radius:34.5px;display:flex;align-items:center;padding:0 10px;box-sizing:border-box;justify-content:space-evenly}
.vedio-file image{height:33px;width:33px}
.item-list{width:150px;height:15px;display:flex;align-items:center}
.time{color:#0d296e;font-size:12px}
.item{height:12px;width:2px;background-color:#3370FF33;margin-left:5px}
.active{background-color:#3370ff}
.item:nth-child(6n-1),.item:nth-child(6n-5){height:4px}
.item:nth-child(6n-2),.item:nth-child(6n-4){height:8px}
.item:nth-child(6n-3){height:14px}
.del-vedio{margin-left:15px}
.del-vedio image{height:25px;width:25px}

//获取应用实例
const app = getApp()
const recorderManager = wx.getRecorderManager()
const innerAudioContext = wx.createInnerAudioContext()
var tempFilePath;
Page({
  data: {
    startVedio: false,
    vedioTime: '0:00',
  },
  //开始录音的时候
  start: function () {
    const options = {
      duration: 20000, //指定录音的时长,单位 ms
      sampleRate: 16000, //采样率
      numberOfChannels: 1, //录音通道数
      encodeBitRate: 96000, //编码码率
      format: 'mp3', //音频格式,有效值 aac/mp3
      frameSize: 50, //指定帧大小,单位 KB
    }
    //开始录音
    recorderManager.start(options);
    this.setData({
      startVedio: true
    })
    recorderManager.onStart((res) => {
      this.setData({
        startVedio: false
      })
      console.log('recorder start')
    });
    //错误回调
    recorderManager.onError((res) => {
      console.log(res);
    })
  },
  //停止录音
  stop: function () {
    recorderManager.stop();
    recorderManager.onStop((res) => {
      this.tempFilePath = res.tempFilePath;
      const ss = Math.floor(res.duration / 1000 % 60)
      const mm = Math.floor(res.duration / 1000 / 60)
      this.setData({
        vedioTime: `${mm}:${ss>10?ss:'0'+ss}`
      })
      console.log('停止录音', res.tempFilePath)
      const {
        tempFilePath
      } = res
    })
  },
  //播放声音
  play: function () {

    innerAudioContext.autoplay = true
    innerAudioContext.src = this.tempFilePath,
      innerAudioContext.onPlay(() => {
        console.log('开始播放')
      })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })

  },
  onLoad() {
    this.ctx = wx.createCameraContext()
  },
  startRecord() {
    this.ctx.startRecord({
      success: (res) => {
        console.log('startRecord')
      }
    })
  },
  stopRecord() {
    this.ctx.stopRecord({
      success: (res) => {
        console.log(res.tempVideoPath)
        this.setData({
          src: res.tempThumbPath,
          videoSrc: res.tempVideoPath
        })
      }
    })
  },
  error(e) {
    console.log(e.detail)
  }
})


http://www.niftyadmin.cn/n/5243105.html

相关文章

【9】PyQt对话框

目录 1. QMessageBox 2. QIputDialog 对话框是为了更好地实现人与程序的交互 对话框主要是完成特定场景下的功能,比如删除确认等 QDialog的子类有QMessageBox、QFileDialog、QFontDialog、QInputDialog等 1. QMessageBox QMessageBox是普通的对话框 代码示例&#xff1a; …

ChatGPT,作为一种强大的自然语言处理模型,具备显著优势,能够帮助您在各个领域取得突破

2023年随着OpenAI开发者大会的召开&#xff0c;最重磅更新当属GPTs&#xff0c;多模态API&#xff0c;未来自定义专属的GPT。微软创始人比尔盖茨称ChatGPT的出现有着重大历史意义&#xff0c;不亚于互联网和个人电脑的问世。360创始人周鸿祎认为未来各行各业如果不能搭上这班车…

ArcGIS模型构建器--制作工具篇

基础知识点&#xff1a; 变量替换 1、要素替换 %name%&#xff0c;name为替换的名字 2、工作空间替换(拷贝给其他人仍可使用) 新建一个工作空间变量workspace %workspace%\%name% 中间数据处理 在模型构建器界面运行模型&#xff0c;会生成中间数据。 中间数据处理方法…

数据挖掘 分类模型选择

选择的模型有&#xff1a; 决策树、朴素贝叶斯、K近邻、感知机 调用的头文件有&#xff1a; import numpy as np import pandas as pd from matplotlib import pyplot as plt from sklearn.linear_model import Perceptron from sklearn.naive_bayes import GaussianNB from s…

AWS re:Invent 2023-亚马逊云科技全球年度技术盛会

一:会议地址 2023 re:Invent 全球大会主题演讲 - 亚马逊云科技从基础设施和人工智能/机器学习创新,到云计算领域的最新趋势与突破,倾听亚马逊云科技领导者谈论他们最关心的方面。https://webinar.amazoncloud.cn/reInvent2023/keynotes.html北京时间2023年12月1日00:30-02:…

vscode 安装插件

打开VSCode编辑器&#xff0c;点击左侧的扩展图标&#xff08;或使用快捷键CtrlShiftX&#xff09;打开扩展面板。 vue插件 在搜索框中输入"Vue"&#xff0c;会显示出一系列与Vue相关的插件。 1. "Vetur"是一个非常强大的Vue开发插件&#xff0c;它提供…

Kubernetes(K8s)DashBoard的使用-11

DashBoard 之前在kubernetes中完成的所有操作都是通过命令行工具kubectl完成的。其实&#xff0c;为了提供更丰富的用户体验&#xff0c;kubernetes还开发了一个基于web的用户界面&#xff08;Dashboard&#xff09;。用户可以使用Dashboard部署容器化的应用&#xff0c;还可以…

PTA校赛算法题十道java、C++详解

目录 7-1 专1 签到 7-2 专2 令人眼花缭乱的字符串 7-3 专3 VALORANT 7-4 专4 吃蛋糕 7-5 专5 Game 7-6 专6 二进制回文串 7-7 专7 度假 7-8 专8 括号匹配Plus 7-9 专9 生成最少叶子树 7-10 专10 禁止超速 这篇文章是基于我们前不久的校赛写的&#xff0c;校赛给的…