uniapp: recording permission check, recording function

1. You can use:plus.navigator.checkPermissionCheck the permissions of the running environment
2. If it is "undetermined", it means that the program has not determined whether this permission can be used , when calling the corresponding API, the system will pop up a prompt box for the user to confirm:plus.audio.getRecorder()

<template>
	<view class="index">
		<view class="title">虚拟人类智能AI已唤醒,可以随时开始聊天啦</view>
		<view class="lists">
			<view class="item" v-for="(item,index) in 13" :key="index">
				<image src="../../static/img/4.png" mode=""></image>
				<view class="right">
					<view class="name">智能AI</view>
					<view class="time">11-20 14:18:27</view>
					<view class="text">内容内容长度内长度长度长度长度</view>
				</view>
			</view>
		</view>

		<view class="footers">
			<view class="submit" 
			@longpress="star" 
			@touchend="stop">
				<image src="../../static/img/5.png" mode=""></image>
				<view class="text1">按住说话</view>
				<view class="text2">Copyright @ 2023 相关内容由人工智能技术生成</view>
			</view>
		</view>
	</view>
</template>

<script>
	const recorderManager = uni.getRecorderManager();
	const innerAudioContext = uni.createInnerAudioContext();
	innerAudioContext.autoplay = true;
	export default {
      
      
		data() {
      
      
			return {
      
      
				audioSrc:'',
				status:0,// 1录制中,0已结束
			}
		},
		onLoad() {
      
      
			var quanxianData = plus.navigator.checkPermission('RECORD');
			if(quanxianData !== 'authorized'){
      
      
				// 先调用一次,提示开启权限
				recorderManager.start({
      
      format:'PCM'});
				setTimeout(()=>{
      
      
					uni.hideLoading();
					this.status = 0;
					recorderManager.stop();
				},500)
			}
			// 录音停止事件
			recorderManager.onStop((res)=> {
      
      
				if(this.status == 1){
      
      
					console.log(res);
					this.audioSrc = res.tempFilePath;
					this.status = 0;
				}
			});
		},
		methods: {
      
      
			star() {
      
      
				// 长按录制前 再次判断当前权限状态。
				var quanxianData = plus.navigator.checkPermission('RECORD');
				if(quanxianData !== 'authorized'){
      
      
					uni.showToast({
      
      
						title: '请前往设置,开启录音权限',
						icon: 'none'
					});
				}else{
      
      
					if(this.status == 1) return
					uni.showLoading({
      
      
						title: '请说出您的问题',
						mask:true
					});
					this.status = 1;
					recorderManager.start({
      
      format:'PCM'});
				}
			},
			stop() {
      
      
				uni.hideLoading();
				recorderManager.stop();
			},
		}
	}
</script>

Guess you like

Origin blog.csdn.net/qq_40745143/article/details/134523876