This article shares the specific code for recording and saving local information for your reference. The specific content is as follows
We can use the MicroPhone class that comes with unity to record, playback and save the recording
The specific code is as follows:
using System; using ; using ; using ; using UnityEngine; public class MicroPhoneManager : MonoBehaviour { public int DeviceLength; /// <summary> /// Recording frequency /// </summary> public string Frequency = "44100"; public int Samplerate = 44100; /// <summary> /// Recording time /// </summary> public int MicSecond = 2; string infoLog = ""; AudioSource _curAudioSource; AudioSource CurAudioSource { get { if (_curAudioSource == null) { _curAudioSource = <AudioSource>(); } return _curAudioSource; } } #region [public Way] /// <summary> /// Get the microphone device /// </summary> public void GetMicrophoneDevice() { string[] mDevice = ; DeviceLength = ; if (DeviceLength == 0) ShowInfoLog("Microphone device not found!"); } /// <summary> /// Start recording /// </summary> public void StartRecordAudio() { (); = false; = true; = (null, true, MicSecond, (Frequency)); while (!((null) > 0)) { } (); ShowInfoLog("Start recording..."); } /// <summary> /// Stop recording /// </summary> public void StopRecordAudio() { ShowInfoLog("End recording..."); if (!(null)) return; (null); (); } /// <summary>s /// Playback recording /// </summary> public void PlayRecordAudio() { if ((null)) return; if ( == null) return; = false; = false; (); ShowInfoLog("Play the recording..."); } /// <summary> /// Print recording information /// </summary> public void PrintRecordData() { if ((null)) return; byte[] data = GetClipData(); #region Users can freely fix recording time int position = (null); var soundata = new float[ * ]; (soundata, 0); var newdata = new float[position * ]; for (int i = 0; i < ; i++) { newdata[i] = soundata[i]; } = (, position, , , false); (newdata, 0); (null); #endregion using (FileStream fs = CreateEmpty( + "/")) { ConvertAndWrite(fs, ); WriteHeader(fs, ); } string infoLog = "total length:" + + " time:" + ; ShowInfoLog(infoLog); } private void WriteHeader(FileStream stream, AudioClip clip) { int hz = ; int channels = ; int samples = ; (0, ); Byte[] riff = .("RIFF"); (riff, 0, 4); Byte[] chunkSize = ( - 8); (chunkSize, 0, 4); Byte[] wave = .("WAVE"); (wave, 0, 4); Byte[] fmt = .("fmt "); (fmt, 0, 4); Byte[] subChunk1 = (16); (subChunk1, 0, 4); UInt16 two = 2; UInt16 one = 1; Byte[] audioFormat = (one); (audioFormat, 0, 2); Byte[] numChannels = (channels); (numChannels, 0, 2); Byte[] sampleRate = (hz); (sampleRate, 0, 4); Byte[] byteRate = (hz * channels * 2); // sampleRate * bytesPerSample*number of channels, here 44100*2*2 (byteRate, 0, 4); UInt16 blockAlign = (ushort)(channels * 2); ((blockAlign), 0, 2); UInt16 bps = 16; Byte[] bitsPerSample = (bps); (bitsPerSample, 0, 2); Byte[] datastring = .("data"); (datastring, 0, 4); Byte[] subChunk2 = (samples * channels * 2); (subChunk2, 0, 4); } private FileStream CreateEmpty(string filepath) { FileStream fileStream = new FileStream(filepath, ); byte emptyByte = new byte(); for (int i = 0; i < 44; i++) //preparing the header { (emptyByte); } return fileStream; } private void ConvertAndWrite(FileStream fileStream, AudioClip clip) { float[] samples = new float[]; (samples, 0); Int16[] intData = new Int16[]; Byte[] bytesData = new Byte[ * 2]; int rescaleFactor = 32767; //to convert float to Int16 for (int i = 0; i < ; i++) { intData[i] = (short)(samples[i] * rescaleFactor); Byte[] byteArr = new Byte[2]; byteArr = (intData[i]); (bytesData, i * 2); } (bytesData, 0, ); } /// <summary> /// Get audio data /// </summary> /// <returns>The clip data.</returns> public byte[] GetClipData() { if ( == null) { ShowInfoLog("Missing audio resources!"); return null; } float[] samples = new float[]; (samples, 0); byte[] outData = new byte[ * 2]; int reScaleFactor = 32767; for (int i = 0; i < ; i++) { short tempShort = (short)(samples[i] * reScaleFactor); byte[] tempData = (tempShort); outData[i * 2] = tempData[0]; outData[i * 2 + 1] = tempData[1]; } if (outData == null || <= 0) { ShowInfoLog("Failed to obtain audio data!"); return null; } return outData; } #endregion void OnGUI() { if (DeviceLength == 0) { if (ShowGUIButton("Get microphone device")) { GetMicrophoneDevice(); } } else if (DeviceLength > 0) { ("Recording frequency:"); Frequency = (Frequency, ( / 5), ( / 20)); (); if (ShowGUIButton("Start recording")) { StartRecordAudio(); } if (ShowGUIButton("End recording")) { StopRecordAudio(); } if (ShowGUIButton("Replay Recording")) { PlayRecordAudio(); } if (ShowGUIButton("Get recording data")) { PrintRecordData(); } (); } (infoLog); } #region [Private Way] /// <summary> /// Show GUI button /// </summary> /// <returns><c>true</c>, if GUI button was shown, <c>false</c> otherwise.</returns> /// <param name="buttonName">Button name.</param> bool ShowGUIButton(string buttonName) { return (buttonName, ( / 20), ( / 5)); } void ShowInfoLog(string info) { infoLog += info; infoLog += "\r\n"; } #endregion }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.