uniapp录音功能

录音功能需要使用到uniapp官方文档里面的API

模版区

javascript 复制代码
<template>
	<view>
		<button @tap="startRecord">开始录音</button>
		<button @tap="endRecord">停止录音</button>
		<button @tap="playVoice">播放录音</button>
	</view>
</template>

js

javascript 复制代码
const recorderManager = uni.getRecorderManager();
const innerAudioContext = uni.createInnerAudioContext();
 
innerAudioContext.autoplay = true;
 
export default {
	data() {
		return {
			text: 'uni-app',
			voicePath: ''
		}
	},
	onLoad() {
		let self = this;
		recorderManager.onStop(function (res) {
			console.log('recorder stop' + JSON.stringify(res));
			self.voicePath = res.tempFilePath;
		});
	},
	methods: {
		startRecord() {
			console.log('开始录音');
 
			recorderManager.start();
		},
		endRecord() {
			console.log('录音结束');
			recorderManager.stop();
		},
		playVoice() {
			console.log('播放录音');
 
			if (this.voicePath) {
				innerAudioContext.src = this.voicePath;
				innerAudioContext.play();
			}
		}
	}
}

现在就是可以进行一个简单的录音,暂停以及播放,再根据需求进行更改即可

还有需要注意的就是在进行真机调试的时候需要判断用户是否打开了录音权限

javascript 复制代码
uni.authorize({
					scope: 'scope.record',
					success() {
						recorderManager.start({
							format: 'mp3'
						});
						t.getTimeInterval();
						uni.setInnerAudioOption({
							obeyMuteSwitch: false
						})
					},
					fail() {
						uni.showModal({
							content: '检测到您没打开录音功能权限,是否去设置打开?',
							confirmText: "确认",
							cancelText: '取消',
							success(res) {
								recorderManager.start();
								t.time = setInterval(this.timer, 50)
							}
						})
					}
				})
相关推荐
贵沫末7 分钟前
React——基础
前端·react.js·前端框架
czhc11400756639 分钟前
Linux 76 rsync
linux·运维·python
aklry18 分钟前
uniapp三步完成一维码的生成
前端·vue.js
Rubin9325 分钟前
判断元素在可视区域?用于滚动加载,数据埋点等
前端
爱学习的茄子26 分钟前
AI驱动的单词学习应用:从图片识别到语音合成的完整实现
前端·深度学习·react.js
用户38022585982426 分钟前
使用three.js实现3D地球
前端·three.js
程序无bug29 分钟前
Spring 面向切面编程AOP 详细讲解
java·前端
zhanshuo29 分钟前
鸿蒙UI开发全解:JS与Java双引擎实战指南
前端·javascript·harmonyos
撰卢1 小时前
如何提高网站加载速度速度
前端·javascript·css·html
10年前端老司机1 小时前
在React项目中如何封装一个可扩展,复用性强的组件
前端·javascript·react.js