<template>
<view class="container">
<!-- 录音状态 -->
<view class="status">{{ status }}</view>
<!-- 按钮 -->
<button type="primary" @click="startRecord">开始录音</button>
<button style="margin: 0 10px" @click="endRecord">结束录音</button>
<button @click="playVoice">播放录音</button>
</view>
</template>
<script>
const recorderManager = uni.getRecorderManager();
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true;
export default {
data() {
return {
status: "就绪",
voicePath: "",
};
},
onLoad() {
recorderManager.onStop((res) => {
console.log("录音完成:", JSON.stringify(res));
this.voicePath = res.tempFilePath;
this.status = "录音已完成,可以播放";
});
recorderManager.onError((err) => {
// 打印最详细的错误对象
console.error("录音底层错误详情:", JSON.stringify(err));
// this.status = "异常:" + JSON.stringify(err);
});
},
methods: {
// 开始录音(去掉报错的 uni.authorize,直接开始录音触发权限)
startRecord() {
try {
console.log("开始录音");
this.status = "正在录音...";
recorderManager.start({
duration: 60000,
sampleRate: 44100,
numberOfChannels: 1,
encodeBitRate: 192000,
format: "aac",
frameSize: 50,
});
} catch (e) {
console.error("startRecord 失败:", e);
this.status = "开始录音失败:" + e.message;
}
},
// 结束录音
endRecord() {
try {
console.log("结束录音");
recorderManager.stop();
this.status = "已停止录音";
} catch (e) {
console.error("endRecord 失败:", e);
}
},
// 播放录音
playVoice() {
console.log("播放录音");
if (this.voicePath) {
innerAudioContext.src = this.voicePath;
innerAudioContext.play();
this.status = "播放中...";
} else {
this.status = "请先录音";
}
},
},
};
</script>
<style scoped>
.container {
padding: 30px;
text-align: center;
}
.status {
margin-bottom: 20px;
font-size: 16px;
color: #333;
}
</style>
2 个回复
Ask小助手
感谢你的反馈,我们来针对这个 bug 进行评审。
1. 反馈内容完整性检查
总体来看,反馈内容基本完整,但复现步骤和实际结果部分填写有误,需要优化。
2. Bug 是否成立
根据知识库中的文档,
uni.getRecorderManager()在鸿蒙平台上是支持的(HBuilderX 4.31 起)。在 ISSUE 系统中,存在一个已知相关问题 没有权限时,双数触发开始录音,不会触发onError事件 ,该问题指出在鸿蒙等平台上,当麦克风权限未正确授予时,录音相关回调(包括 onStop/onError)可能不触发。你的场景很可能与此相关。
官方已针对该问题提供了解决方案:推荐使用开源的
uni-recorder插件来替代原生 API,该插件已对权限和回调逻辑做了适配。因此,这个 bug 成立,属于已知问题,并非误报。
3. 基本概念与平台注意事项
app-plus相关 API(如 plus.audio 等)。你的代码中未出现此类调用,符合规范。建议的解决方案
uni-recorder插件,该插件已处理鸿蒙端的权限和回调异常。插件地址:https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-recorder
使用方式参考 ISUE 中的说明。
recorderManager.start()前已获取麦克风权限(可通过uni.authorize或手动引导用户授权),并监听onError来捕获权限错误。如果你在替换插件后仍有疑问,欢迎补充更详细的操作步骤和实际现象,我们再进一步跟进。
z***@163.com (作者) - 正式进入uni-app开发
已开启麦克风权限,执行了stop后,还是无法回调onStop\onError
要回复问题请先登录或注册
公告
更多>相关问题