Unity录音之AudioClip和byte[]相互转换

11 篇文章 1 订阅

AudioClip转byte[]:

    public byte[] ConvertClipToBytes(AudioClip audioClip)
    {
        float[] samples = new float[audioClip.samples];

        audioClip.GetData(samples, 0);

        short[] intData = new short[samples.Length];

        byte[] bytesData = new byte[samples.Length * 2];

        int rescaleFactor = 32767;

        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;
    }

byte[]转AudioClip:

public AudioClip ConvertBytesToClip(byte[] rawData)
    {
        float[] samples = new float[rawData.Length / 2];
        float rescaleFactor = 32767;
        short st = 0;
        float ft = 0;

        for(int i=0;i<rawData.Length;i+=2)
        {
            st = BitConverter.ToInt16(rawData,i);
            ft = st / rescaleFactor;
            samples[i / 2] = ft;
        }
        
        AudioClip audioClip = AudioClip.Create("mySound", samples.Length, 1, 44100, false, false);
        audioClip.SetData(samples, 0);

        return audioClip;
    }

完整源码:

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System;
using System.Text;

public class Record : MonoBehaviour
{
    public string _microphone;
    
    public AudioClip _audioclip;

    public int frequency = 44100;

    Button submit;

    //当程序唤醒时
    void Awake()
    {
        RecognizeVoice();

        submit = GameObject.Find("Canvas").transform.GetChild(0).gameObject.GetComponent<Button>();
        setText("开始录音");
        //添加按钮事件
        submit.onClick.AddListener(ButtonClick);
    }

    void setText(string text)
    {
        Text btnSubmitTxt = submit.transform.GetChild(0).gameObject.GetComponent<Text>();
        btnSubmitTxt.text = text;
    }

    string getText()
    {
        Text btnSubmitTxt = submit.transform.GetChild(0).gameObject.GetComponent<Text>();
        return btnSubmitTxt.text;
    }

    //按钮事件
    void ButtonClick()
    {
        if(getText()=="开始录音")
        {
            StartRecognize();
            setText("停止录音");
        }
        else
        {
            AudioClip audioClip = ConvertBytesToClip(ConvertClipToBytes(_audioclip));
            StopRecord(audioClip);
            setText("开始录音");
        }
    }
    
    //音频数据转换成字节数组
    public byte[] ConvertClipToBytes(AudioClip audioClip)
    {
        float[] samples = new float[audioClip.samples];

        audioClip.GetData(samples, 0);

        short[] intData = new short[samples.Length];

        byte[] bytesData = new byte[samples.Length * 2];

        int rescaleFactor = 32767;

        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;
    }

    //字节数组转换成音频数据
    public AudioClip ConvertBytesToClip(byte[] rawData)
    {
        float[] samples = new float[rawData.Length / 2];
        float rescaleFactor = 32767;
        short st = 0;
        float ft = 0;

        for(int i=0;i<rawData.Length;i+=2)
        {
            st = BitConverter.ToInt16(rawData,i);
            ft = st / rescaleFactor;
            samples[i / 2] = ft;
        }
        
        AudioClip audioClip = AudioClip.Create("mySound", samples.Length, 1, frequency, false, false);
        audioClip.SetData(samples, 0);

        return audioClip;
    }

    public void StartRecognize()
    {
        Microphone.End(null);//录音前先停掉录音,录音参数为null时采用的是默认的录音驱动
        _audioclip = Microphone.Start(_microphone, true, 30, frequency);
    }

    public void StopRecord(AudioClip clip)
    {
        PlayRecord(clip);
        Microphone.End(null);
    }

    public void PlayRecord(AudioClip clip)
    {
        AudioSource.PlayClipAtPoint(clip, Vector3.zero);
    }
 
    public void RecognizeVoice()
    {
        string[] microPhoneName = Microphone.devices;

        if(microPhoneName.Length > 0)
        {
            _microphone = microPhoneName[0];
        }

        Debug.Log("Microphone:" + _microphone);
    }
}

结语:为什么要相互转换,直接播放不好吗?如果是做语音聊天的话就需要转换成byte[]在网络上传输了,所以还是必要的。

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值