項目實現(xiàn)功能:在unity3D中通過Microphone的API實現(xiàn)錄音功能,并將真正時長的錄音文件以”.wav“格式保存到本地。
環(huán)境:Win10 unity版本:2018.2.15f1 VS版本:2017
界面展示
說明:要提前了解.wav文件的格式
根據(jù).wav文件格式,需要對錄音的聲音流進行重新編碼。
代碼
public class TestMicro : MonoBehaviour {
private bool micConnected = false;//麥克風(fēng)是否連接
private int minFreq, maxFreq;//最小和最大頻率
public AudioClip RecordedClip;//錄音
public AudioSource audioSource;//播放的音頻
public Text Infotxt;//提示信息
public Text Adress;//音頻保存地址
private string fileName;//保存的文件名
if (Microphone.devices.Length <= 0)
Infotxt.text = "缺少麥克風(fēng)設(shè)備!";
Infotxt.text = "設(shè)備名稱為:"+Microphone.devices[0].ToString()+"請點擊Start開始錄音!";
Microphone.GetDeviceCaps(null, out minFreq, out maxFreq);
if (minFreq == 0 && maxFreq == 0)
if (!Microphone.IsRecording(null))
RecordedClip = Microphone.Start(null, false, 60, maxFreq);
Infotxt.text = "正在錄音中,請勿重復(fù)點擊Start!";
Infotxt.text = "請確認麥克風(fēng)設(shè)備是否已連接!";
data = GetRealAudio(ref RecordedClip);
Infotxt.text = "錄音結(jié)束!";
if (!Microphone.IsRecording(null))
audioSource.clip = RecordedClip;
Infotxt.text = "正在播放錄音!";
Infotxt.text = "正在錄音中,請先停止錄音!";
if (!Microphone.IsRecording(null))
fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff");
if (!fileName.ToLower().EndsWith(".wav"))
string path= Path.Combine(Application.persistentDataPath, fileName);//錄音保存路徑
using (FileStream fs = CreateEmpty(path))
fs.Write(data, 0, data.Length);
WriteHeader(fs, RecordedClip); //wav文件頭
Infotxt.text = "正在錄音中,請先停止錄音!";
/// <param name="recordedClip"></param>
public static byte[] GetRealAudio(ref AudioClip recordedClip)
int position = Microphone.GetPosition(null);
if (position <= 0 || position > recordedClip.samples)
position = recordedClip.samples;
float[] soundata = new float[position * recordedClip.channels];
recordedClip.GetData(soundata, 0);
recordedClip = AudioClip.Create(recordedClip.name, position,
recordedClip.channels, recordedClip.frequency, false);
recordedClip.SetData(soundata, 0);
int rescaleFactor = 32767;
byte[] outData = new byte[soundata.Length * 2];
for (int i = 0; i < soundata.Length; i++)
short temshort = (short)(soundata[i] * rescaleFactor);
byte[] temdata = BitConverter.GetBytes(temshort);
outData[i * 2] = temdata[0];
outData[i * 2 + 1] = temdata[1];
Debug.Log("position=" + position + " outData.leng=" + outData.Length);
/// <param name="stream"></param>
/// <param name="clip"></param>
public static void WriteHeader(FileStream stream, AudioClip clip)
int channels = clip.channels;
int samples = clip.samples;
stream.Seek(0, SeekOrigin.Begin);
Byte[] riff = System.Text.Encoding.UTF8.GetBytes("RIFF");
stream.Write(riff, 0, 4);
Byte[] chunkSize = BitConverter.GetBytes(stream.Length - 8);
stream.Write(chunkSize, 0, 4);
Byte[] wave = System.Text.Encoding.UTF8.GetBytes("WAVE");
stream.Write(wave, 0, 4);
Byte[] fmt = System.Text.Encoding.UTF8.GetBytes("fmt ");
Byte[] subChunk1 = BitConverter.GetBytes(16);
stream.Write(subChunk1, 0, 4);
Byte[] audioFormat = BitConverter.GetBytes(one);
stream.Write(audioFormat, 0, 2);
Byte[] numChannels = BitConverter.GetBytes(channels);
stream.Write(numChannels, 0, 2);
Byte[] sampleRate = BitConverter.GetBytes(hz);
stream.Write(sampleRate, 0, 4);
Byte[] byteRate = BitConverter.GetBytes(hz * channels * 2);
stream.Write(byteRate, 0, 4);
UInt16 blockAlign = (ushort)(channels * 2);
stream.Write(BitConverter.GetBytes(blockAlign), 0, 2);
Byte[] bitsPerSample = BitConverter.GetBytes(bps);
stream.Write(bitsPerSample, 0, 2);
Byte[] datastring = System.Text.Encoding.UTF8.GetBytes("data");
stream.Write(datastring, 0, 4);
Byte[] subChunk2 = BitConverter.GetBytes(samples * channels * 2);
stream.Write(subChunk2, 0, 4);
/// <param name="filepath"></param>
private FileStream CreateEmpty(string filepath)
FileStream fileStream = new FileStream(filepath, FileMode.Create);
byte emptyByte = new byte();
for (int i = 0; i < 44; i++) //為wav文件頭留出空間
fileStream.WriteByte(emptyByte);
可執(zhí)行文件(有個bug目前還沒修改,不過不影響使用):
bug:當沒有錄音的時候,點擊save也會出現(xiàn)保存文件的地址,可以在Save()方法中判斷一下if(recordedclip!=null)
可執(zhí)行文件下載地址:https://download.csdn.net/download/qq_40878840/12942233
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。