Unity 字节数组转换成音频

这个是我同事开发过程遇到的问题,被我同事解在这里插入图片描述
决我就复制过来分享给大家

 public static byte[] ConvertClipToBytes(AudioClip clip)
  {
        float[] samples = new float[clip.samples];
            clip.GetData(samples, 0);
        short[] intData = new short[samples.Length];
        //converting in 2 float[] steps to Int16[], //then Int16[] to Byte[]
        byte[] bytesData = new byte[samples.Length * 2];
          //bytesData array is twice the size of
        //dataSource array because a float converted in Int16 is 2 bytes.
        int rescaleFactor = 32767; //to convert float to Int16
        for (int i = 0; i < samples.Length; i++)
          {
            intData[i] = (short)(samples[i] * rescaleFactor);
            byte[] byteArr = new byte[2];
            byteArr = BitConverter.GetBytes(intData[i]);
            byteArr.CopyTo(bytesData, i * 2);
            return bytesData;
            }
        }

下面这个代码块是音频转字节的代码。
如果又帮到你给我点个赞

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值