如何解析Microphone无限时长录制,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
这里提供一个解决思路:
1、将microphone设置为循环录制,即:
Microphone.Start(micName, true,2,16000);
2、每隔一定间隔读取录制好的数据,并将它缓存起来。
这里值得一提的是,如果每隔2秒保存一下音频数据(因为我设置的录制时间是2秒),
因为代码运行也需要时间,会导致音频数据损坏,声音会出现明显的断层现象,所以这里将它分段保存就能解决这种问题,
当麦克风录制的位置大于音频的一半的时候,保存上一段音频,当麦克风录制完时,保存后一段音频。
bool isSaveFirstHalf = true;//将音频从中间分生两段,然后分段保存 int micPosition; while (!isMicRecordFinished) { if (isSaveFirstHalf) { yield return new WaitUntil(() => { micPosition = Microphone.GetPosition(micName);return micPosition > length * 6 / 10 && micPosition < length; });//保存前半段 micDataTemp = new float[length / 2]; micClip.GetData(micDataTemp, 0); micDataList.AddRange(micDataTemp); isSaveFirstHalf = !isSaveFirstHalf; } else { yield return new WaitUntil(() => { micPosition = Microphone.GetPosition(micName); return micPosition > length / 10 && micPosition < length / 2; });//保存后半段 micDataTemp = new float[length/2]; micClip.GetData(micDataTemp, length / 2); micDataList.AddRange(micDataTemp); isSaveFirstHalf = !isSaveFirstHalf; } }
最后处理一下结束时的音频
micPosition = Microphone.GetPosition(micName); if (micPosition <= length)//前半段 { micDataTemp = new float[micPosition/2]; micClip.GetData(micDataTemp, 0); } else { micDataTemp = new float[micPosition - length/2]; micClip.GetData(micDataTemp, length/2); }
3、最后通过保存的数据生成新的音频,即:
AudioClip.Create("RecordClip", micDataList.Count, 1, 16000, false); newAudioClip.SetData(micDataList.ToArray(), 0);
完整代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MicUnlimitedDuration : MonoBehaviour {
public delegate void AudioRecordHandle(AudioClip audioClip);
public AudioSource audioSource;
AudioClip micClip;
bool isMicRecordFinished= true;
List<float> micDataList = new List<float>();
float[] micDataTemp;
string micName;
public void StartMicrophone() {
StopCoroutine(StartMicrophone(null, PlayAudioRecord));
StartCoroutine(StartMicrophone(null, PlayAudioRecord));
}
IEnumerator StartMicrophone(string microphoneName,AudioRecordHandle audioRecordFinishedEvent) {
Debug.Log("Start Mic");
micDataList = new List<float>();
micName = microphoneName;
micClip = Microphone.Start(micName, true,2,16000);
isMicRecordFinished = false;
int length = micClip.channels * micClip.samples;
bool isSaveFirstHalf = true;//将音频从中间分生两段,然后分段保存
int micPosition;
while (!isMicRecordFinished)
{
if (isSaveFirstHalf)
{
yield return new WaitUntil(() => { micPosition = Microphone.GetPosition(micName);return micPosition > length * 6 / 10 && micPosition < length; });//保存前半段
micDataTemp = new float[length / 2];
micClip.GetData(micDataTemp, 0);
micDataList.AddRange(micDataTemp);
isSaveFirstHalf = !isSaveFirstHalf;
}
else
{
yield return new WaitUntil(() => { micPosition = Microphone.GetPosition(micName); return micPosition > length / 10 && micPosition < length / 2; });//保存后半段
micDataTemp = new float[length/2];
micClip.GetData(micDataTemp, length / 2);
micDataList.AddRange(micDataTemp);
isSaveFirstHalf = !isSaveFirstHalf;
}
}
micPosition = Microphone.GetPosition(micName);
if (micPosition <= length)//前半段
{
micDataTemp = new float[micPosition/2];
micClip.GetData(micDataTemp, 0);
}
else
{
micDataTemp = new float[micPosition - length/2];
micClip.GetData(micDataTemp, length/2);
}
micDataList.AddRange(micDataTemp);
Microphone.End(micName);
AudioClip newAudioClip = AudioClip.Create("RecordClip", micDataList.Count, 1, 16000, false);
newAudioClip.SetData(micDataList.ToArray(), 0);
audioRecordFinishedEvent(newAudioClip);
Debug.Log("RecordEnd");
}
public void StopMicrophone()
{
Debug.Log("Stop mic");
isMicRecordFinished = true;
}
void PlayAudioRecord(AudioClip newAudioClip)
{
audioSource.clip = newAudioClip;
audioSource.Play();
}
}
看完上述内容,你们掌握如何解析Microphone无限时长录制的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。