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[]在网络上传输了,所以还是必要的。