您好,登錄后才能下訂單哦!
如何解析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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。