局域网)"/>
树莓派搭建微信小程序服务器传音频(局域网)
微信小程序代码:
微信小程序采用微信开发者工具进行开发,采用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/44100numberOfChannels: 1, //录音通道数,有效值 1/2encodeBitRate: 96000, //编码码率format: 'mp3', //音频格式,有效值 aac/mp3frameSize: 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
树莓派不在身边,之后发
更多推荐
树莓派搭建微信小程序服务器传音频(局域网)
发布评论