内容简介:Unity3D 实现录音小案例
我们都知道,在Unity中有提供麦克风MicroPhone相关的API,今天小编和大家一起来看一下如何实现录制场景中的声音,并且可以在应用中使用。
1.首先,一起回顾一下快速在游戏中创建音频剪辑,官方文档:https://docs.unity3d.com/ScriptReference/AudioClip.Create.html
1.0 第一步,新建项目并且在场景附加一个TestAudio.cs 组件类;
TestAudio.cs 代码如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class TestAudio : MonoBehaviour { public string Frequency="440"; public int Samplerate = 44100; public int Position = 0; void OnGUI() { if (GUI.Button (new Rect (10, 10, 100, 100), "创建音频剪辑")) CreateAudioSource (); GUI.Label (new Rect (10, 200, 100, 100),"Position:"+Position.ToString()); } void CreateAudioSource() { AudioClip myClip = AudioClip.Create("MyFirstSound", Samplerate * 2, 1, Samplerate, true, OnAudioRead, OnAudioSetPosition); AudioSource aud = GetComponent(); aud.clip = myClip; aud.Play(); } void OnAudioRead(float[] data) { int count = 0; while (count < data.Length) { data[count] = Mathf.Sign(Mathf.Sin(2 * Mathf.PI * int.Parse(Frequency) * Position / Samplerate)); Position++; count++; } } void OnAudioSetPosition(int newPosition) { Position = newPosition; } }
1.1 第二步,直接运行看结果;
2. 然后,一起学习如何在Unity中调用MicroPhone相关的接口,官方文档:https://docs.unity3d.com/ScriptReference/30_search.html?q=Microphone.IsRecording
2.0 第一步,新建项目并且在场景中附加一个MicroPhoneManager.cs 组件类;
MicroPhoneManager.cs 代码如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MicroPhoneManager : MonoBehaviour { public int DeviceLength; public string Frequency="440"; public int Samplerate = 44100; public int MicSecond=2; string infoLog=""; AudioSource _curAudioSource; AudioSource CurAudioSource { get{ if (_curAudioSource == null) { _curAudioSource = gameObject.AddComponent (); } return _curAudioSource; } } #region [public Way] /// /// 获取麦克风设备 /// public void GetMicrophoneDevice () { string[] mDevice = Microphone.devices; DeviceLength = mDevice.Length; if (DeviceLength == 0) ShowInfoLog ("找不到麦克风设备!"); } /// /// 开始录音 /// public void StartRecordAudio () { CurAudioSource.Stop (); CurAudioSource.loop = false; CurAudioSource.mute = true; CurAudioSource.clip = Microphone.Start (null, true, MicSecond, int.Parse (Frequency)); while (!(Microphone.GetPosition (null) > 0)) { } CurAudioSource.Play (); ShowInfoLog ("开始录音....."); } /// /// 停止录音 /// public void StopRecordAudio () { ShowInfoLog ("结束录音....."); if (!Microphone.IsRecording (null)) return; Microphone.End (null); CurAudioSource.Stop (); } /// /// 回放录音 /// public void PlayRecordAudio () { if (Microphone.IsRecording (null)) return; if (CurAudioSource.clip == null) return; CurAudioSource.mute = false; CurAudioSource.loop = false; CurAudioSource.Play (); ShowInfoLog ("播放录音....."); } /// /// 打印录音信息 /// public void PrintRecordData () { if (Microphone.IsRecording (null)) return; byte[] data = GetClipData (); string infoLog = "total length:" + data.Length + " time:" + CurAudioSource.time; ShowInfoLog (infoLog); } /// /// 获取音频数据 /// /// The clip data. public byte[] GetClipData () { if (CurAudioSource.clip == null) { ShowInfoLog ("缺少音频资源!"); return null; } float[] samples = new float[CurAudioSource.clip.samples]; CurAudioSource.clip.GetData (samples, 0); byte[] outData = new byte[samples.Length * 2]; int reScaleFactor = 32767; for (int i = 0; i < samples.Length; i++) { short tempShort = (short)(samples [i] * reScaleFactor); byte[] tempData = System.BitConverter.GetBytes (tempShort); outData [i * 2] = tempData [0]; outData [i * 2 + 1] = tempData [1]; } if (outData == null || outData.Length 0) { GUILayout.Label("录音频率:"); Frequency = GUILayout.TextField(Frequency, GUILayout.Width(Screen.width / 5), GUILayout.Height(Screen.height / 20)); GUILayout.BeginVertical(); if (ShowGUIButton("开始录音")) { StartRecordAudio (); } if (ShowGUIButton("结束录音")) { StopRecordAudio (); } if (ShowGUIButton("回放录音")) { PlayRecordAudio (); } if (ShowGUIButton("获取录音数据")) { PrintRecordData (); } GUILayout.EndVertical(); } GUILayout.Label(infoLog); } #region [Private Way] /// /// 显示GUI 按钮 /// /// true, if GUI button was shown, false otherwise. /// Button name. bool ShowGUIButton (string buttonName) { return GUILayout.Button (buttonName, GUILayout.Height (Screen.height / 20), GUILayout.Width (Screen.width / 5)); } void ShowInfoLog (string info) { infoLog += info; infoLog += "\r\n"; } #endregion } }
2.1 第二步,我们可以部署到android/iphone真机上运行,当然也可以直接在PC端运行查看结果;
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- Swift自定义表情键盘+录音
- JavaScript 实现页面中录音功能
- vue实现录音功能(pc端)
- iOS相册,相机,通讯录,录音权限处理
- iOS相册,相机,通讯录,录音权限处理
- 基于react的录音及音频曲线绘制的组件开发
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。