收藏
回答

鸿蒙6.0 调用 RecorderManager 录音大概十几秒左右后出现微信直接崩溃闪退

问题类型 操作系统 操作系统版本 手机型号 微信版本
Bug HarmonyOS 6.0 HUAWEI mate 70 pro 8.0.14.42

就是正常调用RecorderManager wx.getRecorderManager()录音管理器的开始录音,然后等待十几秒就会出现


// 录音对象文件 // 默认录音参数 const DEFAULTY_OPTIONS = { duration: 270000,//指定录音的时长,单位 ms,最大为10分钟(600000),默认为1分钟(60000) sampleRate: 16000,//采样率 numberOfChannels: 1,//录音通道数 encodeBitRate: 64000,//编码码率 format: 'mp3',//音频格式,有效值 aac/mp3/wav/PCM frameSize: 1,//指定帧大小,单位 KB } const Recorder = { recorderManager: null, // 全局唯一的录音管理器 isRecording: false, // 是否在录音 isPause: false, // 是否录音暂停 webAudioContext: null, // WebAudioContext 实例 analyserNode: null, // AnalyserNode 实例 needFrameAnalysis: false, // 是否需要录音切片数据分析 // 各种事件监听 onStartCallback: null, onPauseCallback: null, onResumeCallback: null, onStopCallback: null, onErrorCallback: null, onFrameRecordedCallback: null, // 初始化 init(needFrame = false) { this.needFrameAnalysis = needFrame if (!this.recorderManager) { this.recorderManager = uni.getRecorderManager() // 开始事件 this.recorderManager.onStart(() => { console.log('录音开始') this.isRecording = true // 重置暂停状态 this.isPause = false if (typeof this.onStartCallback === 'function') { this.onStartCallback() } }) // 暂停事件 this.recorderManager.onPause(() => { console.log('录音暂停') this.isPause = true if (typeof this.onPauseCallback === 'function') { this.onPauseCallback() } }) // 恢复事件 this.recorderManager.onResume(() => { console.log('录音恢复') this.isPause = false if (typeof this.onResumeCallback === 'function') { this.onResumeCallback() } }) // 结束事件 this.recorderManager.onStop(res => { console.log('录音结束') this.isRecording = false this.isPause = false if (typeof this.onStopCallback === 'function') { this.onStopCallback(res) } }) // 错误事件 this.recorderManager.onError(error => { console.error('错误信息', error.errMsg) this.isRecording = false this.isPause = false if (typeof this.onErrorCallback === 'function') { this.onErrorCallback(error) } }) // 判断是否需要获取录音分片数据 if (needFrame) { this.initAnalyserNode() this.recorderManager.onFrameRecorded(res => { // res.frameBuffer 是录音数据,转换为 PCM 格式的 ArrayBuffer const self = this this.webAudioContext.decodeAudioData(res.frameBuffer, (buffer) => { // 连接到 AnalyserNode const source = self.webAudioContext.createBufferSource() source.buffer = buffer source.connect(self.analyserNode) source.start() const volumn = self.getVolumeAndWaveform(self) if (typeof self.onFrameRecordedCallback === 'function') { self.onFrameRecordedCallback(volumn) } }, err => { console.error('decodeAudioData fail', err) const volumn = -1 if (typeof self.onFrameRecordedCallback === 'function') { self.onFrameRecordedCallback(volumn) } }) }) } } }, // 初始化AnalyserNode实例 initAnalyserNode() { // 创建 WebAudioContext 实例 if (!this.webAudioContext) this.webAudioContext = wx.createWebAudioContext() // 创建 AnalyserNode 实例 if (!this.analyserNode) this.analyserNode = this.webAudioContext.createAnalyser() // 设置 FFT 大小 this.analyserNode.fftSize = 2048 }, // 获取实时音量和波形数据 getVolumeAndWaveform(self) { const dataArray = new Uint8Array(self.analyserNode.frequencyBinCount) self.analyserNode.getByteTimeDomainData(dataArray) const volumn = self.calculateVolume(dataArray) return volumn }, // 计算音量 calculateVolume(dataArray) { let total = 0 for (let i = 0; i < dataArray.length; i++) { total += Math.abs(dataArray[i] - 128) // 计算每个样本的绝对值 } const rms = Math.sqrt(total / dataArray.length) // 计算均方根 return rms }, // 事件监听器 onStart(callback) { this.onStartCallback = callback; }, onPause(callback) { this.onPauseCallback = callback; }, onStop(callback) { this.onStopCallback = callback; }, onError(callback) { this.onErrorCallback = callback; }, onFrameRecorded(callback) { this.onFrameRecordedCallback = callback }, onResume(callback) { this.onResumeCallback = callback }, // 开始录音 startRecording(options) { if (!this.isRecording) { // 没有录音管理器,初始化创建 if (!this.recorderManager) this.init() const mergedOptions = { ...DEFAULTY_OPTIONS, ...(options || {}) } this.recorderManager.start(mergedOptions) } }, // 处理暂停开始录音 switchRecording() { if (this.isRecording && !this.isPause) this.pauseRecording() else this.resumeRecording() }, // 暂停录音 pauseRecording() { this.recorderManager.pause() this.isPause = true }, // 恢复录音 resumeRecording() { this.recorderManager.resume() this.isPause = false }, // 结束录音 stopRecording() { if (this.isRecording) { this.recorderManager.stop() } }, // 清空事件监听器 clearEventListeners() { this.onStartCallback = null this.onPauseCallback = null this.onStopCallback = null this.onErrorCallback = null }, } export default Recorder
最后一次编辑于  2天前
回答关注问题邀请回答
收藏

1 个回答

  • 智能回答 智能回答 本次回答由AI生成
    2天前
    有用 1
    回复
登录 后发表内容