1***@163.com
1***@163.com
  • 发布:2021-12-31 16:38
  • 更新:2022-03-30 16:47
  • 阅读:1284

【报Bug】uni-app无法录音

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Mac

PC开发环境操作系统版本号: 12.1

HBuilderX类型: 正式

HBuilderX版本号: 3.3.5

手机系统: Android

手机系统版本号: Android 12

手机厂商: 华为

手机机型: nova7

页面类型: vue

vue版本: vue2

打包方式: 云端

项目创建方式: HBuilderX

操作步骤:

点击录音图标开始录音再点击一次结束录音

预期结果:

录音成功,能正常播放

实际结果:

无法进入onStart函数,并且uni.getRecorderManager 为空对象

bug描述:

<!--

  • @Description:
  • @Autor: 龚铱白
  • @CreateDate: Do not edit
  • @Date: 2021-12-29 13:38:56
  • @LastEditTime: 2021-12-31 16:20:21
    -->
    <template>
    <view class="bg-gray">
    <view class="xiangqian5 bg-gray">
    <view style="padding: 30rpx 0 30rpx; width: 100vw">
    <view
    class="bg-white ut-borderz ut-bordery"
    style="margin: 30rpx 30rpx 0 30rpx"

    <view
    class="ut-borderz ut-bordery"
    style="margin: 0 30rpx 0 30rpx; padding: 0"

    <view class="cu-bar"> <text class="text-blue2 text-lg">教师批注</text> <text class="text-blue2 text-lg" @click="toCommit"> 确定</text> </view> <view class="cu-form-group"> <text>语音</text> <view class="normal-voice-box"> <div> <image src="luyin ? yuyin2 : yuyin1" style="width: 160rpx; height: 48rpx"
    @tap="recorder(luyin)"
    </image>
    <image
    class="margin-left" src="kai ? zhanting : bofang" style="width: 48rpx; height: 48rpx"
    @tap="bofang2(kai)"
    </image>
    </div>
    <div class="label" v-if="!kai">播放</div>
    <div class="label" v-if="kai">停止</div>
    </view>
    </view>
    <view class="cu-bar">
    <text class="margin-left">文字批注</text>
    </view>
    <view class="cu-form-group" style="padding: 0 30rpx 0 30rpx">
    <input
    class="
    cu-btn
    block
    ut-input2
    gray
    ut-borderz ut-bordery
    margin-tb-sm
    text-left
    kuangdu1
    "
    type="text"
    @input="danxuan20()"
    v-model="postilWord"
    />
    </view>
    <view class="cu-bar">
    <text class="margin-left">图片批注</text>
    </view>
    <tupiandati
    @xuanzhe="uploadChange" url="imageUrls" upload_count="1" upload_auto="true" upload_file_path="'answer/'" />
    </view>
    </view>
    </view>
    </view>
    </view>
    </template>

<script>
var recorderManager = uni.getRecorderManager();
var audio = uni.createInnerAudioContext();
audio.autoplay = true;
import { getItems } from "../../../common/tool";
import yuyin from "../../../common/yuyin.js";

import { mapState } from "vuex";
export default {
computed: {
...mapState(["juanzilist", "pingjialist", "utUserList"]),
},
watch: {
postilVoice(newVal, oldVal) {
//监听题型属性变化
console.log("postilVoice属性变化", newVal, oldVal);
this.postilVoice = newVal;
this.uprecorder();
},
studentId(newVal) {
if (newVal) {
this.getPizhu();
}
},
},
data() {
return {
taskId: "",
postilWord: "",
kai: false,
content: "",
title: "",
hideName: 2, //开 1 关2
bofang: "/static/jxzd/teacher/bofang.png",
zhanting: "/static/jxzd/teacher/zhanting.png",
yuyin1: "/static/jxzd/teacher/yuyin.png",
yuyin2: "/static/jxzd/teacher/yiyin2.png",
luyin: false,
imageUrls: null,
postilVoice: "",
postilVoice2: "",
};
},
props: {
stemId: {
type: Number,
},
studentId: {
type: Number,
},
},

onLoad() {
let that = this;

audio.onStop(function () {  
  console.log("onStop");  
  //audio.destroy();//这里是播放停止后,销毁实例  
  //audio = null;  
  that.kai = false;  
});  

audio.onEnded(() => {  
  console.log("音频播放结束");  
  console.log("销毁音频实例");  
  //audio.destroy(); //销毁音频实例  
  //audio = null;  
  that.kai = false;  
});  

audio.onError((e) => {  
  console.log("音频播放错误: " + JSON.stringify(e));  
  console.log("销毁音频实例");  
  audio.destroy(); //销毁音频实例  
  //audio = null;  
  that.kai = false;  
});  

recorderManager.onStop(function (res) {  
  // console.log('recorder stop' + JSON.stringify(res));  
  console.log("recorder stop", res);  
  that.postilVoice = res.tempFilePath;  
});  

},

methods: {
play(e) {
let that = this;
console.log("-----", e);

  let src = that.postilVoice;  
  if (src) {  
    //实例化声音  
    audio.src = src; //音频地址  
    audio.play();  
  } else if (!src) {  
    uni.showToast({  
      title: "老师未添加语音批注",  
      icon: "none",  
      duration: 2000,  
    });  
    this.kai = false;  
  }  
},  
start() {  
  console.log("[ 哈哈哈哈 开始路 ] >", recorderManager);  
  recorderManager.onStart(function (res) {  
    console.log("recorder start", res);  
  });  
},  
stop() {  
  recorderManager.onStop(function (res) {  
    console.log("recorder stop", res);  
    that.postilVoice = res.tempFilePath;  
  });  
},  

kaiguan(e) {  
  this.kai = !e;  
  if (this.kai) {  
    this.hideName = 1;  
  } else {  
    this.hideName = 2;  
  }  
},  
bofang2(e) {  
  let that = this;  
  that.kai = !e;  
  if (this.kai) {  
    this.play(this.kai);  
  } else {  
    audio.stop();  
  }  
},  
uprecorder() {  
  let that = this;  
  yuyin.upmp3(  
    this.postilVoice,  
    "voice/",  
    (result) => {  
      console.log("上传mp3result", result);  
      that.postilVoice2 = result;  
      that.$api.msg("录音保存成功");  
    },  
    (err) => {  
      console.log("上传mp3err", err);  
    }  
  );  
},  
recorder(e) {  
  //this.luyin = !e  
  const that = this;  
  that.luyin = !e;  
  if (that.luyin) {  
    let options = {  
      duration: 60000,  
      sampleRate: 22050,  
      format: "mp3",  
    };  
    that.$api.msg("开始录音");  
    recorderManager.start(options);  
    this.start();  
  } else if (!that.luyin) {  
    recorderManager.stop();  
    this.stop();  
    console.log("[ 录音结束 ] >");  
  }  
},  
navTo2(url) {  
  uni.navigateTo({  
    url,  
  });  
},  
toCommit() {  
  const that = this;  
  uni.showModal({  
    title: "提示",  
    content: "确认提交批注?",  
    showCancel: true,  
    confirmText: "确定",  
    success: (e) => {  
      if (e.confirm) {  
        that.doCommit();  
      }  
    },  
  });  
},  
danxuan20() {},  
doCommit() {  
  const that = this;  

  if (!that.postilWord && !that.imageUrls && !that.postilVoice2) {  
    that.$api.msg("请添加批注");  
  } else {  
    let qiuzhuRequest = {  
      //  
      taskId: JSON.parse(getItems("juanzilist")).id  
        ? JSON.parse(getItems("juanzilist")).id  
        : "",  
      stemId: this.stemId ? this.stemId : "", //其实是questionId  
      postilWord: that.postilWord,  
      postilImage: that.imageUrls,  
      postilVoice: that.postilVoice2,  
      studentId: this.studentId,  
    };  
    console.log("that.postilVoice2", that.postilVoice2);  
    // // console.log('qiuzhuRequest',qiuzhuRequest)  
    this.$api  
      .request(  
        "task",  
        "teacherStudentQuestionPostil",  
        qiuzhuRequest,  
        (failres) => {  
          ////// console.log(res)  
          that.$api.msg(failres.message);  
        }  
      )  
      .then((res) => {  
        uni.showModal({  
          content: "提交成功",  
          showCancel: false,  
          confirmText: "好的",  
          success: (e) => {  
            if (e.confirm) {  
              uni.navigateBack({ delta: 1 });  
            }  
          },  
        });  
      });  
  }  
},  
uploadChange(data) {  
  this.imageUrls = data;  
  // // console.log('uploadChange==', data)  
},  
// 查看批注  
getPizhu() {  
  this.$api  
    .request("task", "getTeacherStudentQuestionPostil", {  
      taskId: JSON.parse(getItems("juanzilist")).id  
        ? JSON.parse(getItems("juanzilist")).id  
        : "", //任务ID  
      studentId: this.studentId, //学生ID  
      stemId: this.stemId, //题目ID  
    })  
    .then((res) => {  
      console.log("[ res ] >", res.data);  
      this.imageUrls = res.data.imageUrls;  
      this.postilWord = res.data.postilWord;  
      this.postilVoice2 = res.data.postilVoice;  
    });  
},  

},
};
</script>

<style>
page {
background-color: #f1f1f1;
}
.normal-voice-box {
display: inline-flex;
}
.normal-voice-box .label {
line-height: 24px;
padding-left: 10px;
}
/ //post/addPost &stemId=284&title=2000&isStudentOpen=1&courseId=1135&content=123456 /
</style>

<!--
mDialog.setOnMDialogClickListener(new MDialog.MDialogOnclickListener() {
@Override
public void onYesClick() {
mDialog.dismiss();
String text = commentEditText.getText().toString();
if (TextUtils.isEmpty(text) && TextUtils.isEmpty(question.getPostilImage())
&& TextUtils.isEmpty(question.getPostilVoice())) {
showToast("请添加批注");
return;
}
showLoadDialog("");
question.setPostilWord(text);
Map<String, Object> map = new HashMap<>();
map.put("taskId", getIntent().getIntExtra("taskId", -1));
map.put("stemId", question.getId());
map.put("postilWord", question.getPostilWord());
map.put("postilImage", question.getPostilImage());
map.put("postilVoice", question.getPostilVoice());
mPresenter.commentUpload(map);
}
});
/**

  • 添加批注
  • taskId,stemId,postilWord,postilImage,postilVoice
    */
    public static final String ADD_COMMENT = BASE_URL + "task/teacherQuestionPostil";
    -->
2021-12-31 16:38 负责人:DCloud_Android_ST 分享
已邀请:
FullStack

FullStack - 【插件开发】【专治疑难杂症】【ios上架、马甲包、白包、过审、已成功上架过几百个】【多款插件已上架:https://ext.dcloud.net.cn/publisher?id=22130】【非诚勿扰】QQ:543610866

提供 可以运行的demo

  • 1***@163.com (作者)

    你的意思是整个项目嘛?第一次提交bug 还不太会...

    2021-12-31 16:47

  • FullStack

    回复 1***@163.com: 你运行官方的 Hello uni-app 项目,看有没有回调

    2021-12-31 16:49

  • FullStack

    录音,需要开启麦克风权限

    2021-12-31 16:50

  • 1***@163.com (作者)

    回复 FullStack: 开启了

    2021-12-31 16:54

1***@163.com

1***@163.com (作者)

按照文档测试 进入error

FullStack

FullStack - 【插件开发】【专治疑难杂症】【ios上架、马甲包、白包、过审、已成功上架过几百个】【多款插件已上架:https://ext.dcloud.net.cn/publisher?id=22130】【非诚勿扰】QQ:543610866

第一次会弹出 麦克风 权限,我这边测试没有问题

  • 1***@163.com (作者)

    能撤销之前的权限 重新给权限嘛

    2021-12-31 17:11

  • 1***@163.com (作者)

    或者说您这边的代码方便给我分吗 我自己研究下

    2021-12-31 17:12

  • FullStack

    回复 1***@163.com: 就是你的那份代码

    2021-12-31 17:13

  • FullStack

    你用HBuliderX-Alpha 测试下

    2021-12-31 17:14

  • 9***@qq.com

    我生成的自定义基座为什么没有录音这一项权限?manifest.json里都勾选了,然后我的源码视图和permission.js 给的示例demo一样,但是生成的基座就是没有录音这个权限,调用permission.js,返回的权限一直都是永久被拒绝。。。

    2022-07-28 10:03

1***@163.com

1***@163.com (作者)

解决了,是组件中不能使用录音(组件值得是页面中的components)

  • 阿潇

    我这边也遇到了这样的问题

    2022-02-10 10:25

  • 阿潇

    但是是打包后不能获取录音对象

    2022-02-10 10:26

  • 阿潇

    安卓的为{}

    2022-02-10 10:26

  • 阿潇

    ios是好的

    2022-02-10 10:26

  • 阿潇

    而且没有用组件

    2022-02-10 10:26

2***@qq.com

2***@qq.com

博主 真机调试的时候是好用的,但是云打包之后就会出现不能启用录音功能 请问是什么问题

该问题目前已经被锁定, 无法添加新回复