收藏
回答

录音 片段arraybuffer 怎么转换?

 我小程序开发时调录音api 方法 RecorderManager.onFrameRecorded 回调里拿到的 frameBuffer 通过udp发回电视端播放异常,同事写的安卓app 录音demo拿到的录音片段发回电视播放却是正常的,然后我再小程序起一个udp 服务端,让他用app demo发送录音回到小程序udp server来,小程序server 接收到数据直接传到电视播放也是异常的,所以我怀疑是不是小程序环境里的arraybuffer 有什么不一样

回答关注问题邀请回答
收藏

2 个回答

  • Demons
    Demons
    2022-09-09

    你好,麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)

    2022-09-09
    有用
    回复 2
    • See You Again
      See You Again
      2022-09-09
      2022-09-09
      回复
    • See You Again
      See You Again
      2022-09-09
      2022-09-09
      回复
  • See You Again
    See You Again
    2022-09-09

    <!--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】


    2022-09-09
    有用
    回复
登录 后发表内容