点击录音图标开始录音再点击一次结束录音
- 发布:2021-12-31 16:38
- 更新:2022-03-30 16:47
- 阅读:1488
产品分类: uniapp/App
PC开发环境操作系统: Mac
PC开发环境操作系统版本号: 12.1
HBuilderX类型: 正式
HBuilderX版本号: 3.3.5
手机系统: Android
手机系统版本号: Android 12
手机厂商: 华为
手机机型: nova7
页面类型: vue
vue版本: vue2
打包方式: 云端
项目创建方式: HBuilderX
操作步骤:
预期结果:
录音成功,能正常播放
录音成功,能正常播放
实际结果:
无法进入onStart函数,并且uni.getRecorderManager 为空对象
无法进入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
<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"
class="ut-borderz ut-bordery"
style="margin: 0 30rpx 0 30rpx; padding: 0"
@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";
-->
FullStack - 【插件开发】【专治疑难杂症】【ios上架、马甲包、白包、过审、已成功上架过几百个】【多款插件已上架:https://ext.dcloud.net.cn/publisher?id=22130】【非诚勿扰】QQ:543610866
提供 可以运行的demo
FullStack - 【插件开发】【专治疑难杂症】【ios上架、马甲包、白包、过审、已成功上架过几百个】【多款插件已上架:https://ext.dcloud.net.cn/publisher?id=22130】【非诚勿扰】QQ:543610866
第一次会弹出 麦克风 权限,我这边测试没有问题
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