uniapp使用录音

<template>
  <view class="WH">
    <u--form :model="formData" :rules="rules" ref="uForm" labelPosition="left" labelWidth="110px" class="width paddingL40 paddingLR32 bgWhite text-right-form">
      <u-form-item label="录音:" borderBottom>
        <view class="viaTion">
          <view v-for="(item, index) in soundsList" :key="index" style="display: flex">
            <text @click="playVoice(item)">
              {{ item.fileName }}
            </text>
            <view class="icon_close" @click="soundClose(item, index)"> </view>
          </view>
          <nb-voice-record v-if="soundsList && soundsList.length < 9" @startRecord="start" @endRecord="end" @cancelRecord="cancel"></nb-voice-record>
        </view>
      </u-form-item>
    </u--form>
  </view>
</template>

<script>
export default {
  data() {
    return {
      formData: {},
      soundsList: [],
      rules: {}
    };
  },

  mounted() {},
  methods: {
    //播放录音
    playVoice(item) {
      let _this = this;
      if (item.filePath) {
        const failAudio = uni.createInnerAudioContext();
        failAudio.src = this.$http.config.baseUrl + "/preview/" + item.filePath;
        failAudio.play();
      }
    },
    //删除录音
    soundClose(item, index) {
      this.soundsList.splice(index, 1);
    },
    // 开始录音
    start() {},
    // 结束录音并处理得到的录音文件
    // event中,app端仅有tempFilePath字段,微信小程序还有duration和fileSize两个字段
    end(event) {
      this.sounds = event;
      //发送录音:
      return new Promise((resolve, reject) => {
        let url = "";
        // #ifdef APP-PLUS
        url = this.$http.config.baseUrl + "/api/file/fileUpOne";
        // #endif
        // #ifdef H5
        url = "/api/file/fileUpOne";
        // #endif
        let a = uni.uploadFile({
          url: url,
          filePath: event.tempFilePath,
          name: "file",
          formData: {
            user: "test",
          },
          success: (res) => {
            setTimeout(() => {
              let a = JSON.parse(res.data);
              this.soundsList.push(a.data);
            }, 1000);
          },
          fail: function () {},
        });
      });
    },
    // 用户取消录音
    cancel() {},
  },
};
</script>

<style lang="scss">
.viaTion {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
</style>

下载插件    很全面的~

nbVoiceRecord长按录音动画组件,多端权限判断,可监听开始、结束、取消事件 - DCloud 插件市场

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
uni-app中实现录音功能,可以按照以下步骤进行操作: 1. 首先,在uni-app项目中创建一个录音页面。 2. 在该页面中,引入uni录音API: ```javascript import uniRecorder from '@/uni_modules/uni-recorder/uni-recorder.js'; ``` 3. 在使用uniRecorder的API之前,需要先在manifest.json文件中配置录音相关的权限: ```json "permissions": { "audioRecord": { "desc": "用于录音功能" } } ``` 4. 接下来,可以使用uniRecorder提供的API进行录音操作。例如,可以使用`uniRecorder.startRecord()`方法开始录音使用`uniRecorder.stopRecord()`方法停止录音。 5. 可以通过监听录音状态的变化来获取录音的时长、音频文件路径等信息。例如,可以使用`uniRecorder.onRecordEnd()`方法监听录音结束事件,并在回调函数中处理录音结束后的逻辑。 6. 最后,可以根据需要对录音文件进行保存、上传或其他处理。 以下是一个示例代码,演示了如何在uni-app中实现录音功能: ```javascript <template> <view> <button @tap="startRecord">开始录音</button> <button @tap="stopRecord">停止录音</button> </view> </template> <script> import uniRecorder from '@/uni_modules/uni-recorder/uni-recorder.js'; export default { methods: { startRecord() { uniRecorder.startRecord({ success: (res) => { console.log('录音开始'); }, fail: (err) => { console.log('录音开始失败', err); } }); }, stopRecord() { uniRecorder.stopRecord({ success: (res) => { console.log('录音结束', res.tempFilePath); }, fail: (err) => { console.log('录音结束失败', err); } }); } } } </script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值