当前位置: 首页 > news >正文

uniapp+uView 【详解】录音,自制音频播放器

效果预览

在这里插入图片描述

代码实现

<template><view class="btnListBox"><view class="audioBox" v-if="audioLength"><u-row><u-col span="2"><u--text align='center' :text="currentTime"></u--text></u-col><u-col span="8"><u-slider @change='currentTime_change' v-model="currentTime" :max='audioLength'></u-slider></u-col><u-col span="2"><u--text align='center' :text="audioLength+' 秒'"></u--text></u-col></u-row></view><view v-if="record_status === '准备录音'" @click="startRecord"><u-icon :color='iconColor' name="mic" size='100px'></u-icon><u--text align='center' text="点我开始录音"></u--text></view><view v-if="record_status === '录音中'" @click="endRecord"><Breathing><u-icon :color='iconColor' name="mic" size='100px'></u-icon></Breathing><u--text align='center' text="录音中"></u--text></view><view v-if="record_status === '已录音'" class="row"><view @click="reset"><u-icon :color='iconColor' name="reload" size='100px'></u-icon><u--text align='center' text="重新录制"></u--text></view><view v-if="play_status== '待播放' ||  play_status== '暂停中'" @click="play"><u-icon :color='iconColor' name="play-circle-fill" size='100px'></u-icon><u--text align='center' text="播放试听"></u--text></view><view v-if="play_status== '播放中'" @click="pause"><u-icon :color='iconColor' name="pause-circle-fill" size='100px'></u-icon><u--text align='center' text="暂停播放"></u--text></view></view></view>
</template>
<script>// 创建对象--录音机const recorderManager = uni.getRecorderManager();// 创建对象--录音const audio = uni.createInnerAudioContext()import Breathing from "../components/breathing.vue";export default {components: {Breathing,},data() {return {iconColor: '#3c9cff',// 录音当前播放位置currentTime: 0,audioLength: 0,record_status: '准备录音',play_status: '待播放',voicePath: '',}},onLoad() {let self = this;recorderManager.onStart(res => {});recorderManager.onStop(res => {self.voicePath = res.tempFilePath;audio.src = res.tempFilePath});audio.onCanplay(res => {if (audio.duration) {self.audioLength = audio.duration.toFixed(0)}})audio.onTimeUpdate(res => {self.audioLength = audio.duration.toFixed(0)self.currentTime = audio.currentTime.toFixed(0);});audio.onEnded(res => {self.currentTime = 0self.play_status = '待播放'});},methods: {currentTime_change(new_currentTime) {audio.seek(new_currentTime)},reset() {this.record_status = '准备录音'this.voicePath = ''this.audioLength = 0this.currentTime = 0},startRecord() {recorderManager.start();this.record_status = '录音中'},endRecord() {recorderManager.stop();this.record_status = '已录音'},play() {if (this.voicePath) {audio.play();this.play_status = '播放中'}},pause() {audio.pause();this.play_status = '暂停中'}}}
</script>
<style scoped>.audioBox {width: 100%;}.btnListBox {flex-direction: column;display: flex;justify-content: center;align-items: center;height: 100vh;}.row {width: 100%;display: flex;justify-content: space-evenly;}
</style>

组件 Breathing.vue 见 vue 组件封装 – 【呼吸】动画效果二

官方文档

  • uni.getRecorderManager() | uni-app官网
  • uni.createInnerAudioContext() | uni-app官网

注意事项

电脑上录音的事件响应并不灵敏,建议在手机上预览最终效果。

http://www.lryc.cn/news/297459.html

相关文章:

  • 机器学习---概率图模型(隐马尔可夫模型、马尔可夫随机场、条件随机场)
  • cool 框架 node 后端封装三方Api post请求函数
  • awd总结
  • 【react】react+es6+antd5.13.2+ts,antd表格的操作如何在父组件写?
  • virtio笔记
  • 初始web服务器(并基于idea来实现无需下载的tomcat)
  • 软件文档测试
  • 从零开始手写mmo游戏从框架到爆炸(七)— 消息封装
  • 从Unity到Three.js(画线组件line)
  • LCP 30. 魔塔游戏 - 力扣(LeetCode)
  • 数据结构——单向链表和双向链表的实现(C语言版)
  • TCP和UDP相关问题(重点)(4)——4.使用TCP的协议有哪些?使用UDP的协议有哪些?
  • Python进阶--爬取美女图片壁纸(基于回车桌面网的爬虫程序)
  • [office] excel如何计算毛重和皮重的时间间隔 excel计算毛重和皮重时间间隔方法 #笔记#学习方法
  • Pandas 对带有 Multi-column(多列名称) 的数据排序并写入 Excel 中
  • 如何为Kafka加上账号密码(一)
  • Elasticsearch的Index Lifecycle Management(ILM)
  • 2、学习 Nacos 注册中心
  • Java 如何操作 nginx 服务器上的文件?
  • 时序预测 | MATLAB实现基于CNN-GRU-AdaBoost卷积门控循环单元结合AdaBoost时间序列预测
  • 中创ET4410 台式LCR数字电桥 简单开箱测评
  • 格式化dingo返回内容
  • QGIS编译(跨平台编译)之四十六:minizip编译(Windows、Linux、MacOS环境下编译)
  • MySQL进阶查询篇(1)-索引的类型与创建
  • 【STL】list模拟实现
  • 常用的文件系统、存储类型小整理
  • Java写标准输出进度条
  • leetcode 算法 69.x的平方根(python版)
  • 【golang】24、go get 和 go mod:indrect 与 go mod tidy
  • AI算法工程师-非leetcode题目总结