树莓派搭建微信小程序服务器传音频(局域网)

微信小程序代码:

微信小程序采用微信开发者工具进行开发,采用wx.uploadfile传音频,后端用python、flask写的,很简单,就是request接收一下file,然后用树莓派自带的音频播放器播放一下,在搭建服务器的时候没有用nginx、apache之类的,直接采用flask自带的web server,不是很专业,还请各位大佬谅解,运行时候有时候会发不到树莓派,只要把运行的ip改为0.0.0.0即可由外界进行访问,用耳机口输出,微信小程序js代码如下

// index.js
// 获取应用实例
const app = getApp()

Page({
  onLoad: function (options) {
    var that = this;
    //获取全局唯一的录音管理器 RecorderManager实例
    that.recorderManager = wx.getRecorderManager()
    that.recorderManager.onStop((res) => {
      that.setData({
        tempFilePath: res.tempFilePath // 文件临时路径
      })
      console.log('获取到文件:' + that.data.tempFilePath)
    })
    this.recorderManager.onError((res) => {
      console.log('录音失败了!')
      //console.log(res)
    })
  },
    //开始录音
    start: function () {
      this.recorderManager.start({
        duration: 60000,
        sampleRate: 16000, //采样率,有效值 8000/16000/44100
        numberOfChannels: 1, //录音通道数,有效值 1/2
        encodeBitRate: 96000, //编码码率
        format: 'mp3', //音频格式,有效值 aac/mp3
        frameSize: 50, //指定帧大小
        audioSource: 'auto' //指定录音的音频输入源,可通过 wx.getAvailableAudioSources() 获取
      })
    },
      //录音暂停
  suspend: function () {
    this.recorderManager.pause()
  },
  //继续录音
  continue: function () {
    this.recorderManager.resume()
  },
  //录音停止
  stop: function () {
    this.recorderManager.stop()
  },
  play: function () {
    // 获取innerAudioContext实例
    const innerAudioContext = wx.createInnerAudioContext()
    // 是否自动播放
    innerAudioContext.autoplay = true
    // 设置音频文件的路径
    innerAudioContext.src = this.data.tempFilePath;
    // 播放音频文件
    innerAudioContext.onPlay(() => {
      console.log('开始播放')
    });
    // 监听音频播放错误事件
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  },
  upload:function(){
    console.log('123');
    wx.uploadFile({
      filePath: this.data.tempFilePath,
      name: 'myfile',
      url:'http://192.168.43.222:5000/record/mp3',
      //url:'http://192.168.43.126:5000/record/mp3',
      method: 'post',
      header: {
        "Content-Type": "multipart/form-data" //必须是这个格式
        },
    })
  }


})

html代码

<view class="container">
  <button type="primary" bindtap="start">开始录音</button>
  <button type="primary" bindtap="suspend">录音暂停</button>
  <button type="primary" bindtap="continue">继续录音</button>
  <button type="primary" bindtap="stop">录音停止</button>
  <button type="primary" bindtap="play">播放录音</button>
  <button type="primary" bindtap="upload">上传到树莓派</button>
</view>

后台代码:

配置环境选用的是berryconda
树莓派不在身边,之后发

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值