当前位置:   article > 正文

uniapp实现录音_uniapp 录音

uniapp 录音

目录

1. 录音效果简要

2. 实现的前置条件

3. 实现

  1. 创建一个实例对象

2.  绑定各种事件

4. 其它三点注意事项


1. 录音效果简要

   先把效果图丢下来

如上图所示,主要功能就是录音,获取音频链接,使用音频组件进行播放,话不多说,我们开始实现功能吧

2. 实现的前置条件

   我们用到的是runi.getRecorderManager

   在录音的同时进行定时器记录时间

3. 实现
  1. 创建一个实例对象
  1. // 初始化录音组件
  2. initOnlibeRecord() {
  3. this.recorderManager = uni.getRecorderManager();
  4. let self = this;
  5. // 监听对象的stop事件
  6. this.recorderManager.onStop(async (res) => {
  7. await self.stopAudio();
  8. uni.hideLoading();
  9. });
  10. // 监对象的start事件
  11. this.recorderManager.onStart(function (res) {
  12. self.getOperateTime();
  13. });
  14. },
  15. // 计时
  16. getOperateTime() {
  17. let _this = this;
  18. this.timer = setInterval(function () {
  19. //0.5s为间隔进行记时
  20. _this.onlineConf.tipsTime += 0.5;
  21. }, 500);
  22. },
2.  绑定各种事件

   点击开始录音,用onlineStep记录当前操作

  1. onlineConf: {
  2. onlinePopupShow: false, //在线录音弹框
  3. onlineStep: 1, // 1 开始录音 2录音中 3结束录音 4 播放录音
  4. isfinish: false, // 是否结束录音
  5. isRecord: false, // 是否正在录音
  6. tipsTime: 0, // 录音的时长
  7. },
  1. // 开始录音
  2. startRecord() {
  3. let audioStep = this.onlineConf.onlineStep;
  4. switch (audioStep) {
  5. case 1:
  6. if (this.audioHasClicked) return;
  7. this.audioHasClicked = true;
  8. // 清除计时
  9. this.$set(this.onlineConf, "tipsTime", 0);
  10. this.getRecordPermission();
  11. break;
  12. case 2:
  13. if (this.onlineConf.onlineSte == 3) {
  14. return;
  15. }
  16. this.recorderManager.stop();
  17. console.log("清除计时器");
  18. clearInterval(this.timer);
  19. this.onlineConf.onlineStep++;
  20. console.log("录音已结束");
  21. break;
  22. case 3:
  23. if (this.onlineConf.onlineSte == 4) {
  24. return;
  25. }
  26. this.onlineConf.onlineStep++;
  27. // this.recorderManager.play();
  28. this.$refs.audio.start("audio1");
  29. console.log("录音已播放");
  30. break;
  31. case 4:
  32. this.onlineConf.onlineStep--;
  33. this.isPause = true; // 暂停过
  34. this.$refs.audio.start("audio1");
  35. console.log("录音已暂停");
  36. break;
  37. }
  38. },

3.  获取音频链接

    当我们录音完毕,需要调用这个初始化出来的对象的stop方法,将会返回给我们一个音频链接,我们将该获取到的链接赋值给audio组件的value值,待其初始化后即可进行播放暂停等操作。

4. 其它三点注意事项

1. 该api涉及用户隐私,所以需要获取一下用户是否打开了麦克风权限

  1. // 校验录音权限
  2. getRecordPermission() {
  3. let _this = this;
  4. console.log("获取录音权限1111");
  5. uni.getSetting({
  6. success(res) {
  7. if (res.authSetting["scope.record"]) {
  8. _this.recorderManager.start({ format: "mp3", duration: 600000 });
  9. _this.isFinishAudio = false;
  10. _this.onlineConf.onlineStep++;
  11. _this.audioHasClicked = false;
  12. return true;
  13. } else {
  14. uni.authorize({
  15. scope: "scope.record",
  16. success() {
  17. _this.recorderManager.start({
  18. format: "mp3",
  19. duration: 600000,
  20. });
  21. _this.isFinishAudio = false;
  22. _this.onlineConf.onlineStep++;
  23. _this.audioHasClicked = false;
  24. return true;
  25. },
  26. fail(res) {
  27. _this.audioHasClicked = false;
  28. uni.showToast({
  29. title:
  30. "请点击右上角“…”功能菜单,进入设置界面,打开麦克风权限后,再重新录音",
  31. icon: "none",
  32. duration: 2000,
  33. });
  34. return false;
  35. },
  36. });
  37. }
  38. },
  39. });
  40. },

2. 我们视觉上的录音时间是通过计时器来计算的,但是这个组件有它的录音限制,最长只能录制十分钟

3. 当用户在录制息屏会主动触发该实例对象的stop哦,记得处理一下息屏时的时间,否则容易给用户造成一种看起来还在录制的错觉~

  1. // 息屏处理
  2. destoryTimer() {
  3. if (this.onlineConf.onlineStep == 2) {
  4. this.recorderManager.stop();
  5. clearInterval(this.timer);
  6. this.onlineConf.onlineStep++;
  7. }
  8. },

说到这里就结束啦,有啥问题或者不明白的可以一起交流啊~~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/899893
推荐阅读
相关标签
  

闽ICP备14008679号