Unity录音保存wav
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI; public class Recording : MonoBehaviour { private int Frequency = 16000; //录音频率
private int MicSecond = 300; //保存300秒内的音频(超出则重新开始录制)
public Button bStart;
public Button bStop;
public Button bPlay;
public Text tTip;
public AudioSource au;
private string device; private string recordPath="";
void Awake() {
if (File.Exists("Config/recordPath"))
{
StreamReader file = new StreamReader("Config/recordPath", Encoding.UTF8);
recordPath = file.ReadLine().Trim();
file.Close();
}
}
void Start()
{
device = Microphone.devices[0];
bStart.onClick.AddListener(OnStartClick);
bStop.onClick.AddListener(OnStopClick);
bPlay.onClick.AddListener(OnPlayClick);
}
int recordLength = 0;
DateTime sTime;
void OnStartClick()
{
tTip.text += "\n开始录音....";
au.Stop();
au.loop = false;
au.mute = true;
au.clip = Microphone.Start(device, true, MicSecond, Frequency);
sTime = DateTime.Now;
} void OnStopClick()
{
tTip.text += "\n停止录音!"; if (!Microphone.IsRecording(device))
return;
Microphone.End(device);
au.Stop();
TimeSpan sub = new TimeSpan(DateTime.Now.Ticks-sTime.Ticks);
recordLength = sub.Seconds * Frequency;
WavFromClip("test.wav", au.clip); //将录音保存为wav
}
void OnStopClick(string relativePath)
{
tTip.text += "\n停止录音!"; if (!Microphone.IsRecording(device))
return;
Microphone.End(device);
au.Stop();
TimeSpan sub = new TimeSpan(DateTime.Now.Ticks-sTime.Ticks);
recordLength = sub.Seconds * Frequency;
WavFromClip(recordPath+relativePath+".wav", au.clip); //将录音保存为wav
} void OnPlayClick()
{
if (Microphone.IsRecording(device))
return;
if (au.clip == null)
return;
au.mute = false;
au.loop = false;
au.Play();
tTip.text += "\n播放录音....";
} public void WavFromClip(string WavPosition, AudioClip clip)
{
if (Microphone.IsRecording(null))
return;
Microphone.End(null); using (FileStream fs = CreateEmpty(WavPosition))
{
ConvertAndWrite(fs, au.clip);
WriteHeader(fs, au.clip); //wav文件头
}
} 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;
} private void ConvertAndWrite(FileStream fileStream, AudioClip clip)
{
//Debug.Log("————————"+recordLength);
float[] samples = new float[recordLength];
//float[] samples = new float[clip.samples];
clip.GetData(samples, 0); Int16[] intData = new Int16[samples.Length]; Byte[] bytesData = new Byte[samples.Length * 2]; int rescaleFactor = 32767; //to convert float to Int16 for (int i = 0; i < samples.Length; i++)
{
intData[i] = (short)(samples[i] * rescaleFactor);
Byte[] byteArr = new Byte[2];
byteArr = BitConverter.GetBytes(intData[i]);
byteArr.CopyTo(bytesData, i * 2);
}
fileStream.Write(bytesData, 0, bytesData.Length);
} private 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 two = 2;
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); // sampleRate * bytesPerSample*number of channels, here 44100*2*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); }
}
保存其他格式需要插入相关的动态链接库
Unity录音保存wav的更多相关文章
- Windows Phone 8初学者开发—第21部分:永久保存Wav音频文件
原文 Windows Phone 8初学者开发—第21部分:永久保存Wav音频文件 第21部分:永久保存Wav音频文件 原文地址:http://channel9.msdn.com/Series/Win ...
- Unity录音
上周做过Unity录音,(不知道的可以到网上查找一下,代码挺多的),不过只能录制麦克风的声音,项目需要同时录制背景音和麦克风传进去的声音,经过探索,现已可以录制: 首先需要知道,即使用电脑录音,想录制 ...
- 【一天一个小知识10/20】Unity安卓获取麦克风并录音保存。
2021-10-20 10:42:16 #region 模块信息 // **************************************************************** ...
- unity自动保存项目
原文来自于:http://wiki.unity3d.com/index.php?title=AutoSave#C.23_-_AutoSave.cs (奋斗的菜鸟_1029633680) 很多 ...
- C#文本转语音并保存wav和MP3文件
回顾上次写博客至今都有4个多月了,最近工作比较的忙没时间写博文.以后会多坚持写博文,与大家分享下最近遇到的问题.最近因为项目需要,研究了下用C#开发TTS.下面把大体的思路给大家说说,希望对大家有所帮 ...
- unity文件 PlayerPrefs.SetInt 保存 And PlayerPrefs.GetInt读取
unity文件保存读取PlayerPrefs.SetInt And PlayerPrefs.GetInt using UnityEngine; using System.Collections; ...
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
--iOS多媒体 概览 随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制, ...
- HTML5网页录音和压缩,边猜边做..(附源码)
宣传一下自己的qq群: (暗号:C#交流) 欢迎喜欢C#,热爱C#,正在学习C#,准备学习C#的朋友来这里互相学习交流,共同进步 群刚建,人不多,但是都是真正热爱C#的 我也是热爱C#的 希望大家可以 ...
- iOS开发----音频播放、录音、视频播放、拍照、视频录制
随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制,还是对麦克风.摄像头的操 ...
- PC-1500的代码存入WAV文件
目录 第1章保存 1 1.1 操作 1 1.2 波形说明 4 1.3 波形整形 5 1.4 压缩 8 第2章载入 9 2.1 操作 9 2.2 音量 9 ...
随机推荐
- window 画工业图软件
1.autoCAD 2.visio 3.CorelDraw 4.DrawIO
- TS语法中interface和class的理解
在TS中interface和后端语言如c#中的概念是不一样的,在TS中interface相当于定义了一种类型,是设置自定义类型的方式,区分与基础类型(number.string等),当定义变量时,就可 ...
- JAVA根据时间增加1天
String time = "2021-12-1"; //指定时间 int day = 30;//指定增加天数 SimpleDateFormat sf = new SimpleDa ...
- pyhon_元组(tuple)
定义: 元组中可以存储不一样类型的数据,使用小括号存储数据,中间用逗号进行分割. 元组中的数据定义好后,无法进行修改,有保护数据的目的. 格式化字符串定义多个值的时候,本质上也是元组. 测试: inf ...
- Java执行cmd命令工具类
工具类: public class CmdTask implements Runnable { private String command; private String dirPath; publ ...
- IDEA的主题插件
Xcode-Dark Theme
- pycharm 安装步骤
1.双击安装包 2.点击next 3.选择安装目录后点击next进入下一步 4.根据你电脑的实际情况选择安装32位还是64位 5.勾选 .py 后即可点击next进入下一步 6.默认,直接next 7 ...
- java 操作PDF (spire.pdf)api
https://www.e-iceblue.cn/pdf_java_image_shapes/replace-image-with-new-image-in-pdf-in-java.html mave ...
- STL库相关练习代码
第一题: #include <iostream> #include <vector> #include <iterator> #include <string ...
- maven远程debug
1.修改tomcat服务器配置 打开tomcat/bin/catalina.sh 添加参数 CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_soc ...