unity录音示例(转)

转:http://blog.csdn.net/huutu/article/details/20216613

如果发现录音声音不正常,比如偏小,请将mic的侦听或音量开大

  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO;
  4 using System.Linq;
  5 using System.Text;
  6 using UnityEngine;
  7 using System.Collections;
  8 
  9 [RequireComponent(typeof(AudioSource))]
 10 public class MicroPhoneInput : MonoBehaviour
 11 {
 12 
 13     private static MicroPhoneInput m_instance;
 14 
 15     public float sensitivity = 100;
 16     public float loudness = 0;
 17 
 18     private static string[] micArray = null;
 19 
 20     const int HEADER_SIZE = 44;
 21 
 22     const int RECORD_TIME = 10;
 23 
 24     // Use this for initialization  
 25     void Start()
 26     {
 27     }
 28 
 29     public static MicroPhoneInput getInstance()
 30     {
 31         if (m_instance == null)
 32         {
 33             micArray = Microphone.devices;
 34             if (micArray.Length == 0)
 35             {
 36                 Debug.LogError("Microphone.devices is null");
 37             }
 38             foreach (string deviceStr in Microphone.devices)
 39             {
 40                 Debug.Log("device name = " + deviceStr);
 41             }
 42             if (micArray.Length == 0)
 43             {
 44                 Debug.LogError("no mic device");
 45             }
 46 
 47             GameObject MicObj = new GameObject("MicObj");
 48             m_instance = MicObj.AddComponent<MicroPhoneInput>();
 49         }
 50         return m_instance;
 51     }
 52 
 53     public void StartRecord()
 54     {
 55         audio.Stop();
 56         if (micArray.Length == 0)
 57         {
 58             Debug.Log("No Record Device!");
 59             return;
 60         }
 61         audio.loop = false;
 62         audio.mute = true;
 63         audio.clip = Microphone.Start(null, false, RECORD_TIME, 44100); //22050   
 64         while (!(Microphone.GetPosition(null) > 0))
 65         {
 66         }
 67         audio.Play();
 68         Debug.Log("StartRecord");
 69         //倒计时  
 70         StartCoroutine(TimeDown());
 71 
 72     }
 73 
 74     public void StopRecord()
 75     {
 76         if (micArray.Length == 0)
 77         {
 78             Debug.Log("No Record Device!");
 79             return;
 80         }
 81         if (!Microphone.IsRecording(null))
 82         {
 83             return;
 84         }
 85         Microphone.End(null);
 86         audio.Stop();
 87 
 88         Debug.Log("StopRecord");
 89 
 90     }
 91 
 92     public Byte[] GetClipData()
 93     {
 94         if (audio.clip == null)
 95         {
 96             Debug.Log("GetClipData audio.clip is null");
 97             return null;
 98         }
 99 
100         float[] samples = new float[audio.clip.samples];
101 
102         audio.clip.GetData(samples, 0);
103 
104 
105         Byte[] outData = new byte[samples.Length * 2];
106         //Int16[] intData = new Int16[samples.Length];  
107         //converting in 2 float[] steps to Int16[], //then Int16[] to Byte[]  
108 
109         int rescaleFactor = 32767; //to convert float to Int16  
110 
111         for (int i = 0; i < samples.Length; i++)
112         {
113             short temshort = (short)(samples[i] * rescaleFactor);
114 
115             Byte[] temdata = System.BitConverter.GetBytes(temshort);
116 
117             outData[i * 2] = temdata[0];
118             outData[i * 2 + 1] = temdata[1];
119 
120 
121         }
122         if (outData == null || outData.Length <= 0)
123         {
124             Debug.Log("GetClipData intData is null");
125             return null;
126         }
127         //return intData;  
128         return outData;
129     }
130     public void PlayClipData(Int16[] intArr)
131     {
132 
133         string aaastr = intArr.ToString();
134         long aaalength = aaastr.Length;
135         Debug.LogError("aaalength=" + aaalength);
136 
137         string aaastr1 = Convert.ToString(intArr);
138         aaalength = aaastr1.Length;
139         Debug.LogError("aaalength=" + aaalength);
140 
141         if (intArr.Length == 0)
142         {
143             Debug.Log("get intarr clipdata is null");
144             return;
145         }
146         //从Int16[]到float[]  
147         float[] samples = new float[intArr.Length];
148         int rescaleFactor = 32767;
149         for (int i = 0; i < intArr.Length; i++)
150         {
151             samples[i] = (float)intArr[i] / rescaleFactor;
152         }
153 
154         //从float[]到Clip  
155         AudioSource audioSource = this.GetComponent<AudioSource>();
156         if (audioSource.clip == null)
157         {
158             audioSource.clip = AudioClip.Create("playRecordClip", intArr.Length, 1, 44100, false, false);
159         }
160         audioSource.clip.SetData(samples, 0);
161         audioSource.mute = false;
162         audioSource.Play();
163     }
164     public void PlayRecord()
165     {
166         if (audio.clip == null)
167         {
168             Debug.Log("audio.clip=null");
169             return;
170         }
171         audio.mute = false;
172         audio.loop = false;
173         audio.Play();
174         Debug.Log("PlayRecord");
175 
176     }
177 
178 
179 
180     public float GetAveragedVolume()
181     {
182         float[] data = new float[256];
183         float a = 0;
184         audio.GetOutputData(data, 0);
185         foreach (float s in data)
186         {
187             a += Mathf.Abs(s);
188         }
189         return a / 256;
190     }
191 
192     // Update is called once per frame  
193     void Update()
194     {
195         loudness = GetAveragedVolume() * sensitivity;
196         if (loudness > 1)
197         {
198             Debug.Log("loudness = " + loudness);
199         }
200     }
201 
202     private IEnumerator TimeDown()
203     {
204         Debug.Log(" IEnumerator TimeDown()");
205 
206         int time = 0;
207         while (time < RECORD_TIME)
208         {
209             if (!Microphone.IsRecording(null))
210             { //如果没有录制  
211                 Debug.Log("IsRecording false");
212                 yield break;
213             }
214             Debug.Log("yield return new WaitForSeconds " + time);
215             yield return new WaitForSeconds(1);
216             time++;
217         }
218         if (time >= 10)
219         {
220             Debug.Log("RECORD_TIME is out! stop record!");
221             StopRecord();
222         }
223         yield return 0;
224     }
225 }
MicroPhoneInput.cs
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class Main : MonoBehaviour
 5 {
 6 
 7     // Use this for initialization  
 8     void Start()
 9     {
10 
11     }
12 
13     void OnGUI()
14     {
15         if (GUILayout.Button("Record"))
16         {
17             MicroPhoneInput.getInstance().StartRecord();
18         }
19         if (GUILayout.Button("Play"))
20         {
21             MicroPhoneInput.getInstance().PlayRecord();
22         }
23     }
24 
25     // Update is called once per frame  
26     void Update()
27     {
28 
29     }
30 }
Main.cs

 

posted @ 2016-06-06 09:47  武装三藏  阅读(346)  评论(0)    收藏  举报