uniapp 录音_uni-app小程序录音上传解决方案(后续更新Taro版)
能力依赖
录音功能的要求与限制
与当前页面其余音频播放/录音功能互斥
是否在录音中状态显示
结束/不须要录音时,回收RecorderManager对象
材料
Codeing(结果代码直接看最后)
构造一个简单的DOM结构
先实现小程序的录音功能
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.vue
export 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')) {
// 设置格式为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()
+ afterAudioRecord()
},
},
这样就避免了屡次录音
小程序录音上传
补全咱们的uploadMp3Action方法,咱们使用uni-app的uni.uploadFile()方法来上传录音文件html
uploadMp3Action(e) {
const filePath = e.tempFilePath
const option = {
url: 'xxx',
filePath,
header,
formData: {
filePath
},
name: 'audio',
}
uni.showLoading({
title: '录音上传中...'
})
return await uni.uploadFile(option)
uni.hideloading()
}
最后在页面卸载的时候回收RecorderManager对象
beforeDestroy() {
this.rm = null
}
打完收工~
uniapp 录音_uni-app小程序录音上传解决方案(后续更新Taro版)相关推荐
- uniapp开发的微信小程序如何上传至微信小程序平台-完整简单步骤
一.选中代码进行发行 二.填入微信小程序appid在hbuilderx中点发行 这个id请登录微信小程序号 设置中查看 三.进入微信小程序工具点上传 成功上传. 四.遇到资源超过2M报错 mess ...
- uniapp 超过2m无法上传代码!uni-module太大了,小程序无法上传怎么办?
问题描述: 虽然分包了.但是主包里还有很多uniui的组件,占了2m多,之前一直是点击运行,然后再上传代码.提示的超过2m无法上传. 解决办法: 点击发行,然后上传就可以了. 优化建议: 1.组件按需 ...
- 小程序一次性上传多个本地图片,上拉加载照片以及图片加载延迟解决之道
一:小程序之一次性上传多个本地相片 最近由于项目需要所以学了下小程序,也做了一些东西,随后便有了以下的一些总结了,现在说说如何使用小程序一次性上传多个本地相片. 问题描述 最近做项目的时候要实现一个上 ...
- 微信小程序上传接口php,微信小程序API 上传、下载
微信小程序API 上传.下载 wx.uploadFile(OBJECT) 将本地资源上传到开发者服务器.如页面通过 wx.chooseImage 等接口获取到一个本地资源的临时文件路径后,可通过此接口 ...
- 微信小程序图片上传九宫格拖拽组件
微信小程序图片上传&九宫格拖拽组件 前言 图片上传加九宫格拖拽是一个比较常用的组件,常用于发帖或者评论等内容上传模块,我这篇九宫格拖拽的思路是借鉴了一款优雅的小程序拖拽排序组件实现这篇文章 实 ...
- 微信小程序直接上传文件到阿里云OSS组件封装
微信小程序直接上传文件到OSS 1. 封装公共方法 在根目录utils目录新建一个upload文件夹: // utils/upload/base64.jsvar base64EncodeChars = ...
- 微信小程序:上传的图片显示旋转问题
问题? 开发中遇到微信小程序在上传照片后,会有一定概率旋转,查阅了资料之后,发现是和图片中携带的exif信息中的orientation这个参数有关. 小程序的页面实用webview渲染的,webvie ...
- 微信小程序头像上传(一)
记录一下自己实现微信小程序头像上传的过程. wxml部分( 用了vant组件库 ): <view class="info"><van-imageroundwidt ...
- 【微信小程序】上传文件到阿里云OSS
小程序上传文件到OSS也是利用OSS提供的PostObject接口来实现表单文件上传到OSS 步骤1:配置Bucket跨域访问 客户端进行表单直传到OSS时,会从浏览器向OSS发送带有Origin的请 ...
- 微信小程序视频上传组件直接上传至阿里云OSS
一.微信小程序视频上传组件示例 多视频上传功能,直传OSS,可以直接从OSS删除:组织接受以逗号分隔的视频url地址,自动列出多个视频,但点击上传按钮上传视频后,同样返回以逗号分隔的视频url地址. ...
最新文章
- 程序性能监控分析工具
- 【编译原理】求一个句型短语、直接短语、句柄(一看就懂~!骗小狗)
- 利用css‘content实现指令式tooltip文字提示
- QT绘制水平百分比条形图。
- 利用VC检测程序内存溢出(转)
- html2canvas改成同步,html2canvas转为图片异步转同步问题(记录)
- python 文件操作练习
- ant react 上传_React实战之Ant Design—Upload上传_附件上传
- 利用Glibc库-ORIGIN-提权
- (C)libnet-发送arp/tcp/icmp数据包
- java找出最高工资和下标_Java 8 lambda用于为每个部门选择最高薪资员工
- BZOJ1066: [SCOI2007]蜥蜴
- 2016-05-25 margin-right jsp获取页面流变量 文字颜色
- Java对接微信、支付宝、银联第三方支付
- 2500个常用中文字符 + 130常用中英文字符
- 项目1-员工(绩效)信息管理系统
- LoRa网关市场现状及未来发展趋势
- 微信公众号常见微信吸粉方式
- matlab波形叠加,matlab程序两列波相向传播叠加波形图和动画.doc
- 【训练记录】2013-2014 Petrozavodsk Winter Training Camp, Moscow SU Trinity Contest @homework-7