注:目前只测了安卓8可以用,安卓13不可用,因为安全性问题,安卓把一些权限改成了系统权限,所以不可用,还有一种办法就是把app打包成系统app,目前还未处理。

app.vue文件里面

<script>
	export default {
		onLaunch: function() {
			if (plus.os.name == 'Android') {
				plus.android.requestPermissions(
					[
						// 'android.permission.ACCESS_WIFI_STATE', //手动 挂断和接听 需要这个权限
						// "android.permission.MODIFY_AUDIO_SETTINGS", //手动 挂断和接听 需要这个权限
						// "android.permission.CALL_PHONE", //手动 挂断和接听 需要这个权限
						// "android.permission.READ_PHONE_STATE", //>监听电话状态 需要这个权限
						//  "android.permission.READ_CALL_LOG",//获取号码需要这个权限
						"android.permission.RECORD_AUDIO", // 录音权限
						"android.permission.CAPTURE_AUDIO_OUTPUT", // 录音权限
						// "android.permission.READ_CONTACTS" //获取联系人
						'android.permission.ANSWER_PHONE_CALLS', //手动 挂断和接听 需要这个权限
									"android.permission.MODIFY_AUDIO_SETTINGS", //手动 挂断和接听 需要这个权限
									"android.permission.CALL_PHONE", //手动 挂断和接听 需要这个权限
									"android.permission.READ_PHONE_STATE", //>监听电话状态 需要这个权限
									"android.permission.READ_CALL_LOG", //获取号码需要这个权限
									"android.permission.READ_AUDIO", // 录音权限,
									"android.permission.READ_EXTERNAL_STORAGE",
									"android.permission.WRITE_EXTERNAL_STORAGE",
									"android.permission.RECORD_AUDIO"
					],
					function(resultObj) {
						var result = 0;
						for (var i = 0; i < resultObj.granted.length; i++) {
							var grantedPermission = resultObj.granted[i];
							console.log('已获取的权限:' + grantedPermission);
							result = 1
						}
						for (var i = 0; i < resultObj.deniedPresent.length; i++) {
							var deniedPresentPermission = resultObj.deniedPresent[i];
							console.log('拒绝本次申请的权限:' + deniedPresentPermission);
							result = 0
						}
			  	for (var i = 0; i < resultObj.deniedAlways.length; i++) {
							var deniedAlwaysPermission = resultObj.deniedAlways[i];
							console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
							result = -1
						}
					},
					function(error) {
						console.log('申请权限错误:' + error.code + " = " + error.message);
					});
			}
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>

当前页面index.vue:

<template>
	<view>
		<button @tap="start">开始录音</button>
		<button @tap="end">结束录音</button>

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

<script>
	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("res", res)
				self.end_time = Math.round(new Date().getTime() / 1000);
				let voicePath = res.tempFilePath;
				self.voicePath = voicePath;
				// self.closeTimeOut();

				uni.showToast({
					icon: 'loading',
					title: "请稍后...",
					duration: 0
				});
				// uni.uploadFile({
				// 	url: self.upload_url,
				// 	filePath: voicePath,
				// 	name: "file",
				// 	formData: {
				// 		id: self.phoneInfo.id,
				// 		start_time: self.start_time,
				// 		end_time: self.end_time,
				// 		phone: self.phoneNumber
				// 	},
				// 	header: {
				// 		Authorization: "Bearer " + uni.getStorageSync(EnumData.token)
				// 	},
				// 	success: (res) => {
				// 		// console.log("文件上传成功")
				// 		console.log(res.data);
				// 	},
				// 	fail(err) {
				// 		console.log("文件上传失败")
				// 		console.log(err);
				// 	},
				// 	complete() {
				// 		self.start_time = 0;
				// 		self.end_time = 0;
				// 		uni.hideToast();
				// 	}
				// })
			});
			this.getCallStatus();
		},
		methods: {
			start() {
				console.log('2、通话存在')
				// 延迟录音
				this.start_time = Math.round(new Date().getTime() / 1000);
				recorderManager.start();
			},
			end() {
				console.log("3、电话挂断,上传录音")
				// 结束录音
				recorderManager.stop();
				
			},
			playVoice() {

				console.log(this.voicePath)
				// if (this.voicePath) {
				innerAudioContext.src = this.voicePath;
				console.log("播放录音")
				innerAudioContext.play();
			},

			getCallStatus() {
				let that = this; 
				let maintest = plus.android.runtimeMainActivity();
				let Contexttest = plus.android.importClass("android.content.Context");
				let telephonyManager = plus.android.importClass("android.telephony.TelephonyManager");
				let telManager = plus.android.runtimeMainActivity().getSystemService(Contexttest.TELEPHONY_SERVICE);
				let receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
					onReceive: function(Contexttest, intent) {
						plus.android.importClass(intent);
						let phoneStatus = telManager.getCallState();

						that.callStatus = phoneStatus; //电话状态 0->空闲状态 1->振铃状态 2->通话存在
						switch (phoneStatus) {
							case 0:
								console.log("3、电话挂断,上传录音")
								// 结束录音
								recorderManager.stop();
								break;
							case 1:
								// console.log('1、振铃状态');
								break;
							case 2:
								console.log('2、通话存在')
								// 延迟录音
								that.start_time = Math.round(new Date().getTime() / 1000);
								recorderManager.start();
								break;
						}
					}
				});

				let IntentFilter = plus.android.importClass('android.content.IntentFilter');
				let filter = new IntentFilter();
				filter.addAction(telephonyManager.ACTION_PHONE_STATE_CHANGED);
				maintest.registerReceiver(receiver, filter);
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐