Unity原生实现录音功能
2016-03-11 08:36
483 查看
using UnityEngine; using System.Collections; using System.Collections.Generic; public class MicphoneTest : MonoBehaviour { AudioSource _audio; AudioSource audio { get { if (_audio == null) { _audio = gameObject.AddComponent<AudioSource>(); } return _audio; } } void Start() { string[] ms = Microphone.devices; deviceCount = ms.Length; if (deviceCount == 0) { Log("no microphone found"); } } string sLog = ""; void Log(string log) { sLog += log; sLog += "\r\n"; } int deviceCount; string sFrequency = "10000"; void OnGUI() { if (deviceCount > 0) { GUILayout.BeginHorizontal(); if (!Microphone.IsRecording(null) && GUILayout.Button("Start", GUILayout.Height(Screen.height / 20), GUILayout.Width(Screen.width / 5))) { StartRecord(); } if (Microphone.IsRecording(null) && GUILayout.Button("Stop", GUILayout.Height(Screen.height / 20), GUILayout.Width(Screen.width / 5))) { StopRecord(); } if (!Microphone.IsRecording(null) && GUILayout.Button("Play", GUILayout.Height(Screen.height / 20), GUILayout.Width(Screen.width / 5))) { PlayRecord(); } if (!Microphone.IsRecording(null) && GUILayout.Button("Print", GUILayout.Height(Screen.height / 20), GUILayout.Width(Screen.width / 5))) { PrintRecord(); } sFrequency = GUILayout.TextField(sFrequency, GUILayout.Width(Screen.width / 5), GUILayout.Height(Screen.height / 20)); GUILayout.EndHorizontal(); } GUILayout.Label(sLog); } void StartRecord() { audio.Stop(); audio.loop = false; audio.mute = true; audio.clip = Microphone.Start(null, false, 1, int.Parse(sFrequency)); while (!(Microphone.GetPosition(null) > 0)) { } audio.Play(); Log("StartRecord"); } void StopRecord() { if (!Microphone.IsRecording(null)) { return; } Microphone.End(null); audio.Stop(); } void PrintRecord() { if (Microphone.IsRecording(null)) { return; } byte[] data = GetClipData(); string slog = "total length:" + data.Length + " time:" + audio.time; Log(slog); } void PlayRecord() { if (Microphone.IsRecording(null)) { return; } if (audio.clip == null) { return; } audio.mute = false; audio.loop = false; audio.Play(); } public byte[] GetClipData() { if (audio.clip == null) { Debug.Log("GetClipData audio.clip is null"); return null; } float[] samples = new float[audio.clip.samples]; audio.clip.GetData(samples, 0); byte[] outData = new byte[samples.Length * 2]; int rescaleFactor = 32767; for (int i = 0; i < samples.Length; i++) { short temshort = (short)(samples[i] * rescaleFactor); byte[] temdata = System.BitConverter.GetBytes(temshort); outData[i * 2] = temdata[0]; outData[i * 2 + 1] = temdata[1]; } if (outData == null || outData.Length <= 0) { Debug.Log("GetClipData intData is null"); return null; } return outData; } }
相关文章推荐
- Unity Diffuse Metal Shader Mod
- UNITY3D shader学习心得<一> properties属性接口
- UNITY3D shader学习心得<二> subShader
- UNITY3D shader学习心得<三> Vertex and Fragment Shader
- unity3D中的工程导入eclipse做二次开发
- unity3D中的工程导入eclipse做二次开发
- unity 快速创建小地图
- unity 快速创建小地图
- Unity3D:unity与Android相互传递消息 & unity与ios相互传递消息
- Unity5.0 摄像机 Clear Flags和Culling Mask属性用途详解
- 【转】unity基础问题汇总
- Unity3D 学习笔记4 —— UGUI+uLua游戏框架
- Android NDK编译C/C++结合Unity实现本地数据共享
- Unity 与 OC 不得不说的故事。。
- 【风宇冲】Unity3D性能优化:DrawCall优化
- Unity(64bit)5.1 打开无故出现框框,纹理不见了
- unity与android交互
- Unity3d_AssetPostprocessor简单用法
- iOS+Unity
- Unity Camera Orthographic Size