<!--pages/sing_demo.wxml--> <view class="flex field"> <view>IP</view> <input value="{{ip}}" auto-focus type="text" class="flex_d" data-key="ip" bindinput="onInput" /> </view> <view class="flex field"> <view>PORT</view> <input value="{{port}}" type="text" class="flex_d" data-key="port" bindinput="onInput" /> </view> <button catchtap="startServer">UDP Server</button> <button catchtap="startClient">UDP Client</button> <button catchtap="sendMsg">UDP Send</button> <button catchtap="start">Recorder Start</button> <button catchtap="stop">Recorder Stop</button> <button catchtap="pause">Recorder Pause</button> <button catchtap="resume">Recorder Resume</button> <button catchtap="play">Recorder Play</button> // pages/sing_demo.js let RecorderManager = null, serverUDPSocket = null, UDPSocket = null const AB2String = (arrayBuffer) => { let unit8Arr = new Uint8Array(arrayBuffer); let encodedString = String.fromCharCode.apply(null, unit8Arr), decodedString = decodeURIComponent(escape((encodedString))); //没有这一步中文会乱码 return decodedString; } const innerAudioContext = wx.createInnerAudioContext() Page({ /** * 页面的初始数据 */ data: { localip: '', netmask: '', // 局域网电子掩码 ip: '172.20.149.94', // ip: '0.0.0.0', port: '25200', tempFilePath: '' }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { this.initPage() }, async initPage() { RecorderManager = wx.getRecorderManager() RecorderManager.onStart(res => { console.log('RecorderManager.onStart', res) }) RecorderManager.onStop(res => { console.log('RecorderManager.onStop', res) const { tempFilePath } = res this.setData({ tempFilePath }) }) RecorderManager.onPause(res => { console.log('RecorderManager.onPause', res) }) RecorderManager.onResume(res => { console.log('RecorderManager.onResume', res) }) RecorderManager.onInterruptionBegin(res => { console.log('RecorderManager.onInterruptionBegin', res) }) RecorderManager.onInterruptionEnd(res => { console.log('RecorderManager.onInterruptionEnd', res) }) RecorderManager.onError(res => { console.log('RecorderManager.onError', res) }) RecorderManager.onFrameRecorded(res => { const { frameBuffer } = res console.log('RecorderManager.onFrameRecorded', frameBuffer) this.sendUdpMsg(frameBuffer) }) const { data } = await this.getNetworkType() if (data !== 'wifi') { wx.showToast({ title: '请将网络切换到WiFi' }) return } const { data: { localip, netmask } } = await this.getLocalIP() this.setData({ localip, netmask }) }, destroyPage() { RecorderManager?.stop() UDPSocket?.close() }, onInput({ detail: { value }, currentTarget: { dataset: { key } } }) { this.setData({ [key]: value }) }, startServer() { serverUDPSocket = wx.createUDPSocket() const port = serverUDPSocket.bind(25200) console.log('startUDPServer:', port, serverUDPSocket) this.setData({ ip: 'localhost', port }) serverUDPSocket?.onListening(res => { console.log('serverUDPSocket.onListening', res) }) serverUDPSocket?.onError(err => { console.log('serverUDPSocket.onError', err) }) serverUDPSocket?.onMessage(({ localInfo, remoteInfo, message }) => { console.log('serverUDPSocket.onMessage', localInfo, remoteInfo, message) this.sendUdpMsg(message) }) }, startClient() { const { ip, port } = this.data, p = parseInt(port) UDPSocket = wx.createUDPSocket() console.log('startUDPClient:', UDPSocket) // UDPSocket.connect({ // address: ip, // port: p // }) UDPSocket.bind() UDPSocket?.onMessage(({ localInfo, remoteInfo, message }) => { console.log('UDPSocket.onMessage', localInfo, remoteInfo, AB2String(message)) }) UDPSocket?.onListening(res => { console.log('UDPSocket.onListening', ip, p, res) }) UDPSocket?.onError(err => { console.log('UDPSocket.onError', err) }) }, sendMsg() { this.sendUdpMsg(`Upd msg --- ${Date.now()}`) }, sendUdpMsg(message) { const { ip, port } = this.data UDPSocket?.send({ address: ip, port: parseInt(port), message, complete(res) { console.log('UDPSocket?.send.complete', res) } }) }, start() { RecorderManager.start({ // duration: 5 * 1000, format: 'PCM', frameSize: 1, numberOfChannels: 1, sampleRate: 8000, encodeBitRate: 48000 }) }, stop() { RecorderManager.stop() }, pause() { RecorderManager.pause() }, resume() { RecorderManager.resume() }, play() { const { tempFilePath } = this.data innerAudioContext.src = tempFilePath innerAudioContext.play() // 播放 innerAudioContext.onError(err => { console.log('innerAudioContext.onError', err) }) }, getNetworkType() { return new Promise((resolve, reject) => { wx.getNetworkType({ complete(res) { const { networkType } = res console.log("wx.getNetworkType", res) resolve({ ccRes: true, data: networkType }) } }) }) }, getLocalIP() { return new Promise((resolve, reject) => { wx.getLocalIPAddress({ complete(res) { const { localip = '', netmask = '' } = res console.log("wx.getLocalIPAddress", res) resolve({ ccRes: true, data: { localip, netmask } }) } }) }) }, /** * 生命周期函数--监听页面隐藏 */ onHide() { this.destroyPage() }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } }) 具体机型【iphone 8 plus】、微信版本号【8.0.27】、系统版本号【iOS15.6.1】
录音 片段arraybuffer 怎么转换?我小程序开发时调录音api 方法 RecorderManager.onFrameRecorded 回调里拿到的 frameBuffer 通过udp发回电视端播放异常,同事写的安卓app 录音demo拿到的录音片段发回电视播放却是正常的,然后我再小程序起一个udp 服务端,让他用app demo发送录音回到小程序udp server来,小程序server 接收到数据直接传到电视播放也是异常的,所以我怀疑是不是小程序环境里的arraybuffer 有什么不一样
2022-09-09您好,现在我这边还是会出现这个问题,小程序跟app已在开放平台里绑定到同一开发者账号下,就是新用户完全没有登录过同一开发者账号下的不同主体(app、小程序、公众号等),这种情况下调用 <button class="confirm" open-type='getUserInfo' lang="zh_CN" bindgetuserinfo="onGotUserInfo">确定</button> 让用户去授权登录,onGotUserInfo 中返回的encryptedData还是没有unionID
code2Session获取unionid,出现有时获取不到,有时获取的到的情况通过小程序传入code调用后台的code2Session方法,unionid有时候获取得到,有时候获取不到的情况,开放平台都已经将小程序和同主体的公众号绑定了
2019-09-02