uniapp制作h5端小程序端一键录音功能

uniapp制作h5端小程序端一键录音功能
如图
在这里插入图片描述
上代码

<template>
	<view class="record-container">
		<button @touchstart="startRecord" @touchend="endRecord">按住说话,松开结束</button>

		<button @tap="playVoice">播放录音</button>

		<view class="status">录音中{{intIntervalTime}}s>>></view>
	</view>
</template>

<script>
	const recorderManager = uni.getRecorderManager();

	const innerAudioContext = uni.createInnerAudioContext();

	innerAudioContext.autoplay = true;

	export default {
		data() {
			return {
				text: "uni-app",

				voicePath: "",

				isRecord: false, // 记录状态,录音中或者是未开始

				intervalTime: 0,

				timer: null
			};
		},

		onLoad() {
			let self = this;

			recorderManager.onStop(function(res) {
				console.log("录音停止了" + JSON.stringify(res)); //返回录音的临时保存地址, 可用于后面的播放
				self.voicePath = res.tempFilePath;
			});
		},

		computed: {
			intIntervalTime() {
				// 用于显示整数的秒数

				console.log(Math.round(this.intervalTime));

				return Math.round(this.intervalTime);
			}
		},

		methods: {
			startRecord() {
				this.timer = setInterval(() => {
					this.intervalTime += 0.5;

					if (this.intervalTime >= 0.5 && !this.isRecord) {
						//如果用户录制的时间太短,就不会去开启录音, 因为有个bug: recorderManager.stop()在短时间内开启在关闭的话,实际上他还在不停地录音,不知道你们有没有遇到过

						console.log("开始录音");

						this.isRecord = true;

						this.intervalTime = 0;

						recorderManager.start({
							format: "mp3"
						});
					}
				}, 500);
			},

			endRecord() {
				if (this.intervalTime <= 0.5) {
					console.log("录音太短了!!!");
				}

				clearInterval(this.timer);

				if (this.isRecord) {
					setTimeout(() => {
						recorderManager.stop();

						this.isRecord = false;

						console.log(this.isRecord);
					}, 500); //延迟小段时间停止录音, 更好的体验
				}
			},

			playVoice() {
				console.log("播放录音");

				if (this.voicePath) {
					console.log(this.voicePath)
					innerAudioContext.src = this.voicePath;

					innerAudioContext.play();
				}
			}
		}
	};
</script>

<style>
	.record-container {
		padding-top: 100upx;
	}

	.status {
		text-align: center;
	}
</style>

/* 语音输入 */
	.Voice_input{
		border-bottom: 1px solid rgba(215,215,215,0.50);
	}
	.audiodiv{
		padding-bottom: 50px;
	}
	.Voice_title{
		height: 41px;
		line-height: 41px;
		font-family: PingFangSC-Regular;
		font-size: 16px;
		color: #1F1F26;
	}
	.speak_btn{
		width: 178px;
		height: 30px;
		background-image: linear-gradient(180deg, #FFFFFF 0%, #F1F1F1 100%);
		border: 1px solid #E8E8E8;
		border-radius: 15.5px;
		margin: 0 auto;
		text-align: center;
		line-height: 30px;
		font-family: PingFangSC-Regular;
		font-size: 14px;
		color: #AAAAAA;
		text-align: center;
		margin-bottom: 15px;
	}
	.talkbtn{
	  color: #cccccc;
	  font-size: 11px;
	  border: 1rpx solid #cccccc;
	  border-radius: 40rpx;
	  margin-top: 5px;
	  position: absolute;
	  left: 50%;
	  transform:translate(-50%,0);
	  background: #f2f6fa;
	
	}
	.showaudio{
	  position: fixed;
	  left: 800rpx;
	  top: 0;
	  width: 213px;
	    height: 28px;
	}
	

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值