Unity获取麦克风录音的方法
2017-04-06 18:09
561 查看
话不多说直接上代码
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[] samp
4000
les = 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;
}
}接口都是留好的,要修改的话调用一下就可以。
声音已经导出成了byte数据,想存想传,实现一下就好,Good Luck!
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[] samp
4000
les = 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;
}
}接口都是留好的,要修改的话调用一下就可以。
声音已经导出成了byte数据,想存想传,实现一下就好,Good Luck!
相关文章推荐
- as3 获取MicrophoneExample的activityLevel值而不产生本地回声(麦克风的声音不传送到本地扬声器)解决方法
- Unity 获取Terrain大小的方法
- Unity 关于GetComponentsInChildren 利用扩展方法如何避免获取父物体
- 视听技术之耳机麦克风二合一接口录音监听完美设置(smartAudio插口配置方法,解决电脑耳机插孔无反应)
- Unity获取未激活游戏对象的方法
- Unity的AOP拦载,获取方法名,参数值,返回类型
- Unity(C#)获取当前运行exe路径的方法
- 使用Unity原生方法进行录音即使播放基于UGUI
- [Unity&C#]获取某个方法执行的时间
- Unity将麦克风获取的音频实时的处理成可视化的音波
- 利用MIXER获取麦克风录音音量
- Unity获取object所有属性的一个方法,一些界面上没有开放的属性可以用该方法编辑
- Android录音时获取分贝值的方法代码实例
- 使用WindowsAPI获取录音音频的方法
- unity获取某个方法执行时间
- Android 实时获取麦克风录音的音量大小
- 使用WindowsAPI获取录音音频的方法
- Unity热更新之C#反射动态获取类属性及方法
- Unity获取当前手机电量的方法
- 使用WindowsAPI获取录音音频的方法