uni-app uni.getRecorderManager() 始终返回空对象

uni-app uni.getRecorderManager() 始终返回空对象

开发环境 版本号 项目创建方式
Windows Windows 11 专业版 HBuilderX
Android Android 5.0

操作步骤:

  • onLoad 执行

预期结果:

  • 不为空对象

实际结果:

  • {}

bug描述:

主页通过uni.getRecorderManager() 在onLoad 获取时候返回对象都为 {} 不知道什么原因 官网的demo也不行 代码提示是有的 不知都为啥 一直返回空 升级成最新3.98 也一样 有官方解释下吗


更多关于uni-app uni.getRecorderManager() 始终返回空对象的实战教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复

刚写了个例子,可以录音,播放 <template>
<view class="content">
<image class="logo" src="/static/logo.png" />
<view class="text-area">
<button @tap=“startRecord”>开始录音</button>
<button @tap=“endRecord”>停止录音</button>
<button @tap=“playVoice”>播放录音</button>
</view>
</view>
</template>

<script setup lang="ts"> import { onLoad } from '@dcloudio/uni-app' import {ref} from 'vue' let voicePath = ref(null) let recorderManager = null let innerAudioContext = null onLoad(() => { recorderManager = uni.getRecorderManager(); innerAudioContext = uni.createInnerAudioContext(); innerAudioContext.autoplay = true; console.log('recorderManager') console.log(recorderManager) recorderManager.onStop(function (res) { console.log('recorder stop' + JSON.stringify(res)); voicePath.value = res.tempFilePath }); }) function startRecord() { console.log('开始录音'); recorderManager.start(); } function endRecord() { console.log('录音结束'); recorderManager.stop(); } function playVoice() { console.log('播放录音'); if (voicePath.value) { innerAudioContext.src = voicePath.value; innerAudioContext.play(); } } </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>

更多关于uni-app uni.getRecorderManager() 始终返回空对象的实战教程也可以访问 https://www.itying.com/category-93-b0.html


不清楚是否是因为 app 权限的原因,这块是不是可以整理放入文档内,后续直接放链接就成,你熟悉这个吗

回复 DCloud_UNI_OttoJi: 可能不是权限的问题,先让这个用户跑一下这个例子,看功能是否正常

解决了吗

在使用 uni-appuni.getRecorderManager() 方法时,如果始终返回空对象,可能是由于以下几个原因导致的。你可以按照以下步骤进行排查和解决:

1. 检查运行环境

uni.getRecorderManager() 方法在部分平台上可能不支持,或者需要特定的运行环境。确保你在支持该方法的平台上运行,例如微信小程序、H5 等。

2. 检查权限

在某些平台上,录音功能需要用户授权。确保你已经获取了录音权限。你可以在调用 uni.getRecorderManager() 之前,先检查并请求录音权限。

uni.authorize({
    scope: 'scope.record',
    success() {
        const recorderManager = uni.getRecorderManager();
        console.log(recorderManager);
    },
    fail() {
        console.log('录音权限未授权');
    }
});

3. 检查代码逻辑

确保你在正确的时机调用 uni.getRecorderManager()。例如,在 onLoadonReady 生命周期中调用。

onReady() {
    const recorderManager = uni.getRecorderManager();
    console.log(recorderManager);
}

4. 检查平台差异

不同平台对 uni.getRecorderManager() 的实现可能有所不同。确保你使用的平台支持该方法,并且没有其他限制。

5. 调试输出

在调用 uni.getRecorderManager() 后,输出返回的对象,检查是否有其他属性或方法可用。

const recorderManager = uni.getRecorderManager();
console.log(recorderManager);

6. 更新 uni-app 版本

确保你使用的 uni-app 版本是最新的,旧版本可能存在一些 bug 或兼容性问题。

7. 检查插件或依赖

如果你使用了某些插件或依赖,确保它们没有影响到 uni.getRecorderManager() 的正常使用。

8. 参考官方文档

查阅 uni-app 官方文档,确保你正确使用了 uni.getRecorderManager() 方法。

const recorderManager = uni.getRecorderManager();

recorderManager.onStart(() => {
    console.log('recorder start');
});

recorderManager.onStop((res) => {
    console.log('recorder stop', res);
});

recorderManager.start({
    duration: 10000,
    sampleRate: 44100,
    numberOfChannels: 1,
    encodeBitRate: 192000,
    format: 'mp3'
});
回到顶部