• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧
您的位置:首页 > CMS教程 >建站教程 > uni-app小程序录音上传的解决方案

uni-app小程序录音上传的解决方案

作者:站长图库 字体:[增加 减小] 来源:互联网

站长图库向大家介绍了uni-app,小程序,录音上传,解决方案等相关知识,希望对您有所帮助

能力依赖

RecorderManager 全局唯一的录音管理器


录音功能的要求与限制

与当前页面其他音频播放/录音功能互斥

是否在录音中状态显示

结束/不需要录音时,回收RecorderManager对象

材料

可以/结束 录音 录音中


Codeing(结果代码直接看最后)

构造一个简单的DOM结构

<image @click="recordAction" :src="recordImg" class="record"/>

先实现小程序的录音功能

import iconRecord from '../../static/images/icon_record.png'import iconRecording from '../../static/images/icon_recording.png'// ...data() {    recordImg: iconRecord, // 录音按钮的图标    rm: null, // 录音管理器},// ...mounted() {    if (this.rm === null) {        // 录音管理器如果没有初始化就先初始化        this.rm = uni.getRecorderManager()    }    // 绑定回调方法    this.rm.onStart((e) => this.onStart(e))    this.rm.onPause((e) => this.onPause(e))    this.rm.onResume((e) => this.onResume(e))    this.rm.onInterruptionBegin((e) => this.onInterruptionBegin(e))    this.rm.onInterruptionEnd((e) => this.onInterruptionEnd(e))    this.rm.onError((e) => this.onError(e))},// ...methods: {    // ...    recordAction() {        if (this.recordImg === iconRecord) {            // 设置格式为MP3,最长60S,采样率22050            this.rm.start({                duration: 600000,                format: 'mp3',                sampleRate: 22050,            })            // 开始录音后绑定停止录音的回调方法            this.rm.onStop((e) => this.onStop(e))        } else if (this.recordImg === iconRecording) {            this.rm.stop()        },    },    onStart(e) {        console.log('开始录音', this.question, this.subQuesIndex)        this.recordImg = iconRecording        console.log(e)    },    onPause(e) {        console.log(e)        afterAudioRecord()    },    onResume(e) {        console.log(e)    },    onStop(e) {        console.log(e)        this.recordImg = iconRecord        // 结束录音之后上传录音        this.uploadMp3Action(e)    },    onInterruptionBegin(e) {      console.log(e)    },    onInterruptionEnd(e) {        console.log(e)    },    onError(e) {        console.log(e)    },    uploadMp3Action(e) {        // TODO uploadMp3    },},

只能同时有一个录音,与音频播放互斥

globalData中增加两个属性audioPlaying,audioRecording

// src/App.vueexport default {    globalData: {          // 保证全局只有一个音频处于播放状态且录音与播放操作互斥        audioPlaying: false,        audioRecording: false,    },    // ...},

Util中增加判断方法

// src/lib/Util.js// 结束录音之后释放录音能力export function afterAudioRecord() {    getApp().globalData.audioRecording = false}// 结束音频播放之后释放音频播放能力export function afterAudioPlay() {    getApp().globalData.audioPlaying = false}/** * 判断是否可以录音或者播放 * @param {string} type play | record */export function beforeAudioRecordOrPlay(type) {    const audioPlaying = getApp().globalData.audioPlaying    const audioRecording = getApp().globalData.audioRecording    if (audioPlaying ||audioRecording) {        uni.showToast({            title: audioPlaying ? '请先暂停其他音频播放' : '请先结束其他录音',            icon: 'none'        })        return false    } else {        if (type === 'play') {            getApp().globalData.audioPlaying = true        } else if (type === 'record') {            getApp().globalData.audioRecording = true        } else {            throw new Error('type Error', type)        }        return true    }}

改造原有recordAction方法

import { beforeAudioRecordOrPlay, afterAudioRecord} from '../../lib/Utils';// ...recordAction() {-  if (this.recordImg === iconRecord) {+  if (this.recordImg === iconRecord && beforeAudioRecordOrPlay('record')) {      
  


 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 如何快速搭建uni-app项目?两种搭建方法分享
  • 移动uni-app项目怎么实现发送位置的地图交互
  • uni-app中怎么开发一个全局弹层组件(代码示例)
  • 遇到的uni-app的坑(uni-easyinput清空值,datetimerange置空)
  • uni-app介绍全局样式引入和底部导航栏开发
  • 解决uni-app入坑集合的一种方案
  • 聊聊怎么将小程序项目转为uni-app项目
  • 浅析uni-app中怎么提交form表单?(代码解析)
  • 一起分析uni-app怎么实现上传图片
  • 看看使用uni-app如何编写一个五子棋小游戏

相关文章

  • PHP+jQuery+MySql实现红蓝投票功能
  • Discuz!教程之删除注释云平台JS,加快Discuz访问
  • Illustrator绘制心形效果的粉色海报
  • js获取UEditor富文本编辑器中的图片地址
  • 给dedecms软件列表页添加下载次数的方法
  • PHP重定向如何实现数据不丢失?
  • Centos7下宝塔面板PHP7.3怎么安装sqlsrv扩展
  • Javascript如何实现json字符串与对象转换
  • 浅谈php正则替换函数preg_replace的用法
  • Photoshop设计颗粒质感艺术字教程

文章分类

  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧

最近更新的内容

    • Photoshop制作简洁时尚的形象主页
    • Photoshop给外景草地女孩添加柔美逆光效果
    • 浅析Vue中的Vue.set和this.$set,看看使用场景!
    • PhotoShop制作炫光抽象层次视觉效果文字教程
    • robots.txt 语法详解:*、$、?等字符的含义及用法
    • 帝国CMS下载地址不用弹窗修改方式
    • Photoshop绘制清新绿色立体桔子按钮
    • Photoshop绘制蓝色风格的游戏手柄
    • Photoshop设计卡通风格的云彩效果
    • WordPress国内网速慢加速及防DDOS攻击快速CF切换

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有