1. 项目实现功能:在unity3D中通过Microphone的API实现录音功能,并将真正时长的录音文件以”.wav“格式保存到本地。
  2. 环境:Win10     unity版本:2018.2.15f1             VS版本:2017
  3. 界面展示
  4. 说明:要提前了解.wav文件的格式

  5. 根据.wav文件格式,需要对录音的声音流进行重新编码。
  6. 代码
    using UnityEngine;
    using UnityEngine.UI;
    using System;
    using System.IO;public class TestMicro : MonoBehaviour {private bool micConnected = false;//麦克风是否连接private int minFreq, maxFreq;//最小和最大频率public AudioClip RecordedClip;//录音public AudioSource audioSource;//播放的音频public Text Infotxt;//提示信息public Text Adress;//音频保存地址private string fileName;//保存的文件名private byte[] data;private void Start(){if (Microphone.devices.Length <= 0){Infotxt.text = "缺少麦克风设备!";}else{Infotxt.text = "设备名称为:"+Microphone.devices[0].ToString()+"请点击Start开始录音!";micConnected = true;Microphone.GetDeviceCaps(null, out minFreq, out maxFreq);if (minFreq == 0 && maxFreq == 0){maxFreq = 44100;}}}/// <summary>/// 开始录音/// </summary>public void Begin(){if (micConnected){if (!Microphone.IsRecording(null)){RecordedClip = Microphone.Start(null, false, 60, maxFreq);Infotxt.text = "开始录音!";}else{Infotxt.text = "正在录音中,请勿重复点击Start!";}}else{Infotxt.text = "请确认麦克风设备是否已连接!";}}/// <summary>/// 停止录音/// </summary>public void Stop(){data = GetRealAudio(ref RecordedClip);Microphone.End(null);Infotxt.text = "录音结束!"; }/// <summary>/// 播放录音/// </summary>public void Player(){if (!Microphone.IsRecording(null)){audioSource.clip = RecordedClip;audioSource.Play();Infotxt.text = "正在播放录音!";}else{Infotxt.text = "正在录音中,请先停止录音!";}}/// <summary>/// 保存录音/// </summary>public void Save(){if (!Microphone.IsRecording(null)){ fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff");if (!fileName.ToLower().EndsWith(".wav")){//如果不是“.wav”格式的,加上后缀fileName += ".wav";}string path= Path.Combine(Application.persistentDataPath, fileName);//录音保存路径print(path);//输出路径Adress.text = path;using (FileStream fs = CreateEmpty(path)){fs.Write(data, 0, data.Length);WriteHeader(fs, RecordedClip); //wav文件头}}else{Infotxt.text = "正在录音中,请先停止录音!"; }}/// <summary>/// 获取真正大小的录音/// </summary>/// <param name="recordedClip"></param>/// <returns></returns>public static byte[] GetRealAudio(ref AudioClip recordedClip){int position = Microphone.GetPosition(null);if (position <= 0 || position > recordedClip.samples){position = recordedClip.samples;}float[] soundata = new float[position * recordedClip.channels];recordedClip.GetData(soundata, 0);recordedClip = AudioClip.Create(recordedClip.name, position,recordedClip.channels, recordedClip.frequency, false);recordedClip.SetData(soundata, 0);int rescaleFactor = 32767;byte[] outData = new byte[soundata.Length * 2];for (int i = 0; i < soundata.Length; i++){short temshort = (short)(soundata[i] * rescaleFactor);byte[] temdata = BitConverter.GetBytes(temshort);outData[i * 2] = temdata[0];outData[i * 2 + 1] = temdata[1];}Debug.Log("position=" + position + "  outData.leng=" + outData.Length);return outData;}/// <summary>/// 写文件头/// </summary>/// <param name="stream"></param>/// <param name="clip"></param>public static void WriteHeader(FileStream stream, AudioClip clip){int hz = clip.frequency;int channels = clip.channels;int samples = clip.samples;stream.Seek(0, SeekOrigin.Begin);Byte[] riff = System.Text.Encoding.UTF8.GetBytes("RIFF");stream.Write(riff, 0, 4);Byte[] chunkSize = BitConverter.GetBytes(stream.Length - 8);stream.Write(chunkSize, 0, 4);Byte[] wave = System.Text.Encoding.UTF8.GetBytes("WAVE");stream.Write(wave, 0, 4);Byte[] fmt = System.Text.Encoding.UTF8.GetBytes("fmt ");stream.Write(fmt, 0, 4);Byte[] subChunk1 = BitConverter.GetBytes(16);stream.Write(subChunk1, 0, 4);UInt16 one = 1;Byte[] audioFormat = BitConverter.GetBytes(one);stream.Write(audioFormat, 0, 2);Byte[] numChannels = BitConverter.GetBytes(channels);stream.Write(numChannels, 0, 2);Byte[] sampleRate = BitConverter.GetBytes(hz);stream.Write(sampleRate, 0, 4);Byte[] byteRate = BitConverter.GetBytes(hz * channels * 2); stream.Write(byteRate, 0, 4);UInt16 blockAlign = (ushort)(channels * 2);stream.Write(BitConverter.GetBytes(blockAlign), 0, 2);UInt16 bps = 16;Byte[] bitsPerSample = BitConverter.GetBytes(bps);stream.Write(bitsPerSample, 0, 2);Byte[] datastring = System.Text.Encoding.UTF8.GetBytes("data");stream.Write(datastring, 0, 4);Byte[] subChunk2 = BitConverter.GetBytes(samples * channels * 2);stream.Write(subChunk2, 0, 4);}/// <summary>/// 创建wav格式文件头/// </summary>/// <param name="filepath"></param>/// <returns></returns>private FileStream CreateEmpty(string filepath){FileStream fileStream = new FileStream(filepath, FileMode.Create);byte emptyByte = new byte();for (int i = 0; i < 44; i++) //为wav文件头留出空间{fileStream.WriteByte(emptyByte);}return fileStream;}
    }

  7. 可执行文件(有个bug目前还没修改,不过不影响使用):

bug:当没有录音的时候,点击save也会出现保存文件的地址,可以在Save()方法中判断一下if(recordedclip!=null)

可执行文件下载地址:https://download.csdn.net/download/qq_40878840/12942233

有问题欢迎私信交流!

unity3D实现录音功能,并将真实录音时长保存至本地(不能用可私信,附可执行文件下载地址)相关推荐

  1. iOS 录音,获取录音时长及格式转换

    转载请注明出处!!! 在APP中,我们也会遇到调用录音的功能,那么如何录音呢?并且在iOS中录音格式是wav或者caf格式的,和安卓不通用,为了达到通用的效果,我们还需要把他转换成通用格式.近期我遇到 ...

  2. 备忘录想要查看录音时长怎么办?

    备忘录这种类型的软件有很多种,有的是通过文字记录事情,有的是通过录音记录事情,也有一些功能比较全面的,可以兼具以上两种功能.当备忘录中储存了录音之后,想要查看录音文件的时长应该怎么操作呢?以敬业签为例 ...

  3. android 获取录音时长_录音转文字,支持安卓和IOS和PC

    在生活过过程中,我们需要用笔去记录一些比较重要的会议记录,但是在这个过程中我们肯定会遗漏一些关键的要点 还有电话录音的时候我们需要做案例分析的时候,如果反反复复去听感觉大脑没有那么容易反应过来,对于分 ...

  4. php录音时长统计,音频app阅读时长统计分析

    描述 分析学习喜马拉雅app阅读时长统计机制,并应用到自己app中去 分析 使用抓包工具charles观测app统计发送时机,发现切换音频源,或本地缓存有阅读数据进入首页app时发送(nyx/v2/t ...

  5. 微信小程序:录音功能(授权、统计时长、上传)

    最近呢,用小程序做了个录音的功能,接下来呢,与大家一起分享一下我的开发心得,希望能帮到大家.那我们就继续向下

  6. Oracle 安装时执行setup时出现乱码报错以及Oracle 11 Windows x64版下载地址和教程链接

    如图,出现了这个错误. 解决方案: 1. 首先保证你的路径没有中文和一些特殊字符 2. 我本人亲自实验解决方案: 我们知道,下载好的两个文件都需要解压到同一个文件夹,如下图所示: 所以你需要确认,你已 ...

  7. android 录音获取分贝变化,Android录音时获取分贝值的方法代码实例

    public class MediaRecorderDemo { private final String TAG = "MediaRecord"; private MediaRe ...

  8. android录音程序闪退,【报Bug】调用录音时,app闪退了

    今天在华为nova 4e 型号MAR-AL00 安卓9版本测试, 调用录音api时,app闪退了,但是在小米手机测试,不会出现,使用的老模板模式编译模式,代码如下: 语音描述(录音时长最大为10分钟) ...

  9. Android 录音(录音时为pcm,然后转为MP3)

    项目中用的评论回复功能,录制语言时为pcm格式,然后转换为MP3格式: package zhiji.dajing.com.util;import android.media.AudioFormat; ...

最新文章

  1. android程序设计期末试题b,《Android程序设计》期末试题B.doc
  2. 主成分分析(PCA) Java
  3. Flash 与物理笔记:简单的小球重力模拟
  4. java 随机数 分布_java – 随机数的分布
  5. arcgis公里坐标转经纬度_高德api交通态势爬取及可视化利用 python+arcgis
  6. Go 语言框架 Gin 练习1
  7. 【译】Deep Learning with Knowledge Graphs
  8. vue 模板 html 表达式,Vue 模板template、指令directive、修饰符
  9. range作用于对象global失败_彻底弄懂JavaScript作用域问题
  10. android 控件置于屏幕最底端
  11. C语言 读取文件中特定数据
  12. PHP设计模式——解释器模式
  13. oracle mysql数据库管理工具下载_Oracle数据库管理工具PC版-Oracle数据库管理工具下载v15.0.21.0(32/64)-IE浏览器中文网站...
  14. 如何使用Arduino构建3D打印的电子动画眼睛
  15. 刷机-3.71M33升级到3.71M33-3的方法
  16. 四川安湖科技:抖音中视频的方案是什么
  17. 使用google翻译免费翻译文档,这里以pdf为例
  18. 在centos上安装pycharm
  19. Javase多态(对多态的理解、多态的体现)
  20. 爆米花现象_爆米花雨是什么梗 看了电影昆池岩你就懂了

热门文章

  1. MySQL设置简单密码
  2. JavaScript进阶
  3. groupby函数分组统计
  4. Python开发系列课程(11) - 面向对象编程进阶
  5. 高新技术企业申请容易吗?如何提高申报通过的机率?
  6. Redis删除大Key
  7. NameNode处理上报block块逻辑分析
  8. Web前端知识CSS(清浮动的方法、CSS精灵图、滑动门)
  9. 万物互联时代的操作系统报告 附下载地址
  10. Android中实现平铺图片