当前位置:   article > 正文

uniapp小程序--录音功能

uniapp小程序--录音功能

关键代码

录音弹窗样式

  1. <uni-popup ref="popup" type="bottom" background-color="#fff" @change="change">
  2. <view class="popup-content">
  3. <view class="audioBox">00:00</view>
  4. <view class="audios">
  5. {{audios}}
  6. </view>
  7. <view class="models">
  8. <view class="delAudio">
  9. 取消
  10. </view>
  11. <view class="abtnBox">
  12. <button class="startBtn" @tap="startRecord" v-if="!audioStatus"></button>
  13. <button class="stopBtn" @tap="endRecord" v-if="audioStatus">
  14. <image src="../../static/stop.png" mode="widthFix"></image>
  15. </button>
  16. </view>
  17. <view class="delAudios">
  18. 取消
  19. </view>
  20. </view>
  21. <!-- <button @tap="files">上传</button> -->
  22. </view>
  23. </uni-popup>

部分css(时间久了css没加注释QAQ):

  1. <style>
  2. .popup-content {
  3. padding: 20px;
  4. }
  5. .audioBox {
  6. width: 100%;
  7. padding: 30px 0;
  8. display: flex;
  9. justify-content: center;
  10. align-items: center;
  11. font-size: 14px;
  12. color: #505050;
  13. }
  14. .audios {
  15. width: 100%;
  16. font-size: 14px;
  17. padding: 10px 0;
  18. color: #505050;
  19. display: flex;
  20. justify-content: center;
  21. align-items: center;
  22. }
  23. .startBtn {
  24. width: 46px;
  25. height: 46px;
  26. border-radius: 50%;
  27. background-color: #FF6D4D;
  28. }
  29. .stopBtn {
  30. width: 46px;
  31. height: 46px;
  32. border-radius: 50%;
  33. background-color: #fff;
  34. display: flex;
  35. justify-content: center;
  36. align-items: center;
  37. }
  38. .stopBtn>image {
  39. width: 100%;
  40. }
  41. .abtnBox {
  42. width: 50px;
  43. height: 50px;
  44. border-radius: 50%;
  45. border: 1px solid #FF6D4D;
  46. display: flex;
  47. justify-content: center;
  48. align-items: center;
  49. }
  50. </style>

关键实现代码

  1. const imgInfo = reactive({
  2. imgs: ''
  3. })
  4. innerAudioContext.autoplay = true;
  5. const audios = ref('点击开始录音')
  6. const player = ref(false)
  7. const audioShow = ref(false)
  8. const startRecord = () => {
  9. console.log('开始录音');
  10. audios.value = '正在录音'
  11. audioStatus.value = true
  12. player.value = false
  13. recorderManager.start({format:'mp3'});
  14. console.log(voicePath.value);
  15. }
  16. const endRecord = () => {
  17. console.log('录音结束');
  18. audios.value = '点击开始录音'
  19. audioStatus.value = false
  20. player.value = true
  21. recorderManager.stop();
  22. console.log(voicePath.value);
  23. recorderManager.onStop(function(res) {
  24. console.log('recorder stop' + JSON.stringify(res));
  25. voicePath.value = res.tempFilePath;
  26. files()
  27. });
  28. console.log(voicePath.value);
  29. }
  30. const playVoice = () => {
  31. console.log('播放录音');
  32. if (voicePath.value) {
  33. innerAudioContext.src = voicePath.value;
  34. innerAudioContext.play();
  35. }
  36. console.log(voicePath.value);
  37. }
  38. const files = () => {
  39. console.log('上传');
  40. console.log(voicePath.value);
  41. uni.uploadFile({
  42. url: 'https://xxxxx/Fileimg/file', //仅为示例,并非真实接口地址。
  43. filePath: voicePath.value,
  44. name: 'file',
  45. formData: {
  46. 'user': 'test',
  47. },
  48. success: function(res) {
  49. console.log(res.data);
  50. if(res.data.code == 1){
  51. console.log(res.data);
  52. imgInfo.imgs = JSON.parse(res.data)
  53. console.log(imgInfo.imgs.data.url);
  54. wx.showToast({
  55. title: '上传中',
  56. icon: 'loading',
  57. duration: 700
  58. })
  59. setTimeout(()=>{
  60. wx.showToast({
  61. title: '上传成功',
  62. icon: 'success',
  63. duration: 700
  64. })
  65. audioShow.value = true
  66. popup.value?.close()
  67. },700)
  68. }else{
  69. wx.showToast({
  70. title: '网络错误',
  71. icon: 'error',
  72. duration: 100
  73. })
  74. audioShow.value = true
  75. popup.value?.close()
  76. }
  77. }
  78. });
  79. }

总结一下:主要实现靠官方提供的api,在官网查就有,本篇仅为本人日常所用技术的总结,不做教学(狗头)

ヾ( ̄▽ ̄)Bye~Bye~

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号