前言
我们项目最近要做一个聊天系统,我们使用的是tencent的IM.sdk。其中里面涉及一个语音聊天,im.sdk中支持传输语音文件,但是语音文件需要我们自己录制,我看了IM.sdk的源码,里面是原生代码实现的录音和播放,但是我们用unity的话,其实官方 已经给我们带了一个microphone类,类实现麦克风的使用。所以下面就是一个简单粗暴的录音,存储,播放 的一个小demo。
数据转化
unity的录音数据使用float[]来存储,但是我们的文件系统使用的是byte[]类存储,所以我们需要做一个数据的转化函数。
- float2byte函数
byte[] float2byte(float[] floats)
{
byte[] outData = new byte[floats.Length * 2];
int reScaleFactor = 32767;
for (int i = 0; i < floats.Length; i++)
{
short tempShort = (short) (floats[i] * reScaleFactor);
byte[] tempData = BitConverter.GetBytes(tempShort);
outData[i * 2] = tempData[0];
outData[i * 2 + 1] = tempData[1];
}
return outData;
}
- byte2float函数
float[] byte2float(byte[] bytes)
{
float reScaleFactor = 32768.0f;
float[] data = new float[bytes.Length / 2];
for (int i = 0; i < bytes.Length; i += 2)
{
short s;
if (BitConverter.IsLittleEndian) //小端和大端顺序要调整
s = (short) ((bytes[i + 1] << 8) | bytes[i]);
else
s = (short) ((bytes[i] << 8) | bytes[i + 1]);
// convert to range from -1 to (just below) 1
data[i / 2] = s / reScaleFactor;
}
完整代码
using System;
using System.Collections;
using UnityEngine;
public class MicrophoneInput : MonoSingleton<MicrophoneInput>
{
protected AudioSource audioSource;
public int lengthSec = 10;
public int frequency = 44100;
void Awake()
{
audioSource = this.GetComponent<AudioSource>();
}
public IEnumerator RequestMicrophoneAuth()
{
yield return Application.RequestUserAuthorization(UserAuthorization.Microphone);
}
public void StartRecord()
{
Debug.Log("开始录音");
audioSource.Stop();
audioSource.loop = false;
audioSource.mute = true;
audioSource.clip = Microphone.Start(null, false, 10, 44100);
audioSource.Play();
}
public int StopRecord()
{
int length = Microphone.GetPosition(null);
Microphone.End(null);
audioSource.Stop();
return length;
}
public void SaveAudioFile(string filePath, int length)
{
if (Microphone.IsRecording(null))
return;
byte[] data = GetClipData(audioSource, length);
if (data == null)
{
return;
}
FileUtility.SafeWriteAllBytes(filePath, data);
string infoLog = "total length:" + data.Length + " time:" + audioSource.time;
Debug.Log(infoLog);
}
public void ReadAudioFile(string filePath)
{
byte[] bytes = FileUtility.SafeReadAllBytes(filePath);
float[] samples = byte2float(bytes);
var clip = audioSource.clip;
audioSource.Stop();
audioSource.clip = AudioClip.Create(clip.name, samples.Length, clip.channels, clip.frequency, false);
audioSource.clip.SetData(samples, 0);
audioSource.mute = false;
audioSource.Play();
}
byte[] GetClipData(AudioSource source, int length)
{
if (length < 10)
{
Debug.Log("录音文件太短");
return null;
}
float[] samples = new float[length];
source.clip.GetData(samples, 0);
byte[] outData = float2byte(samples);
return outData;
}
byte[] float2byte(float[] floats)
{
byte[] outData = new byte[floats.Length * 2];
int reScaleFactor = 32767;
for (int i = 0; i < floats.Length; i++)
{
short tempShort = (short) (floats[i] * reScaleFactor);
byte[] tempData = BitConverter.GetBytes(tempShort);
outData[i * 2] = tempData[0];
outData[i * 2 + 1] = tempData[1];
}
return outData;
}
float[] byte2float(byte[] bytes)
{
float reScaleFactor = 32768.0f;
float[] data = new float[bytes.Length / 2];
for (int i = 0; i < bytes.Length; i += 2)
{
short s;
if (BitConverter.IsLittleEndian) //小端和大端顺序要调整
s = (short) ((bytes[i + 1] << 8) | bytes[i]);
else
s = (short) ((bytes[i] << 8) | bytes[i + 1]);
// convert to range from -1 to (just below) 1
data[i / 2] = s / reScaleFactor;
}
return data;
}
}
示例demo
using UnityEngine;
using UnityEngine.UI;
public class MicroTest : MonoBehaviour
{
public Button startBtn;
public Button stopBtn;
void Awake()
{
startBtn.onClick.AddListener(this.OnStartClick);
stopBtn.onClick.AddListener(this.OnStopClick);
}
/// <summary>
/// 开始录制
/// </summary>
void OnStartClick()
{
Debug.Log("OnStartClick");
if (!Application.HasUserAuthorization(UserAuthorization.Microphone))
{
StartCoroutine(MicrophoneInput.Instance.RequestMicrophoneAuth());
return;
}
MicrophoneInput.Instance.StartRecord();
}
/// <summary>
/// 停止录制
/// </summary>
private void OnStopClick()
{
Debug.Log("OnStopClick");
int length = MicrophoneInput.Instance.StopRecord();
var path = Application.persistentDataPath + "/audios/micro.m4a";
MicrophoneInput.Instance.SaveAudioFile(path, length);
PlayAudio();
}
/// <summary>
/// 播放录音
/// </summary>
private void PlayAudio()
{
var path = Application.persistentDataPath + "/audios/micro.m4a";
//读取本地文件播放
MicrophoneInput.Instance.ReadAudioFile(path);
}
}