uniapp上传音频文件到服务器

视频教程地址:

【uniapp录音上传组件,将录音上传到django服务器】 https://www.bilibili.com/video/BV1wi4y1p7FL/?share_source=copy_web&vd_source=e66c0e33402a09ca7ae1f0ed3d5ecf7c

  • uniapp 录制音频文件上传到django服务器保存到服务器 ,要求uniapp显示录制进度波形图 请提供完整代码
  • 以下是一个示例代码,演示了如何在uniapp中录制音频文件并将其上传到Django服务器,并在uniapp中显示录制进度波形图。

    在uniapp的页面中,你可以使用uni-voice组件来录制音频,并使用uni-upload组件来上传文件。在Django服务器端,你可以使用Django的文件上传功能来保存音频文件。

    uniapp页面代码(index.vue):

     
<template>
  <view class="container">
    <view class="waveform" :style="{ width: waveformWidth + 'px' }"></view>
    <view class="controls">
      <button @tap="startRecording" :disabled="isRecording">开始录制</button>
      <button @tap="stopRecording" :disabled="!isRecording">停止录制</button>
      <button @tap="uploadFile" :disabled="!recordedFile">上传文件</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      isRecording: false,
      recordedFile: null,
      waveformWidth: 0,
      audioContext: null,
      audioRecorder: null,
      audioChunks: [],
    };
  },
  mounted() {
    this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
  },
  methods: {
    startRecording() {
      this.isRecording = true;
      this.audioChunks = [];
      navigator.mediaDevices.getUserMedia({ audio: true })
        .then((stream) => {
          this.audioRecorder = new MediaRecorder(stream);
          this.audioRecorder.addEventListener('dataavailable', (event) => {
            this.audioChunks.push(event.data);
            this.drawWaveform();
          });
          this.audioRecorder.start();
        })
        .catch((error) => {
          console.error('Error accessing microphone:', error);
        });
    },
    stopRecording() {
      this.isRecording = false;
      this.audioRecorder.stop();
      this.audioRecorder.stream.getTracks().forEach((track) => track.stop());
      this.audioRecorder = null;
      this.recordedFile = new File(this.audioChunks, 'recorded_audio.wav', { type: 'audio/wav' });
    },
    drawWaveform() {
      const waveformElement = this.$refs.waveform;
      const canvas = waveformElement.getContext('2d');
      const width = waveformElement.width;
      const height = waveformElement.height;
      const data = new Uint8Array(this.audioChunks.reduce((acc, chunk) => acc + chunk.byteLength, 0));
      let offset = 0;
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王家视频教程图书馆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值