- Home /
Microphone doesn't record (or save) sometimes Android Unity 20.1.10f1
I am using Unity to build an Android application. In the application I record my voice and save it to a wave file. However sometimes the mic doesn't record anything or the SavWav class doesn't save the recording file. It works fine half of the time but half of the other time it doesn't. I have tried to replicate it on a shorter piece of code but the program worked fine in it. I am suspecting that it has something to do with interference from some other part of the application or overload on the process but I can't figure it out. What my app is supposed to do is to record the persons voice while he is moving the phone and then when he finishes moving it the phone stops recording the audio and sends it to google cloud speech-to-text service and displays the words the person said. I tried replicating it but with only a simple touch or no-touch mechanism but the program seemed fine.
public class Recorder
{
public const int STANDART_REC_LENGTH = 15;
public readonly static string STANDART_REC_PATH = Application.persistentDataPath + "/recording.wav";
public string currRecordingPath = "";
public int currRecordingLen = STANDART_REC_LENGTH;
private bool isRecording = false;
public bool record = false;
public TextMeshPro verbose;
private AudioClip recording;
private DateTime startTime;
private object locker = new object();
//Returns a trimmed version of the <originalClip>
//originalClip - the originial clip
//startPosSec - The starting position in seconds (eg: 1, 0.34, 2782.12, etc.)
//lengthSec - The new length of the AudioClip in seconds (eg: 1, 0.34, 2782.12, etc.)
private AudioClip TrimAudioClip(AudioClip originalClip, float startPosSec, float lengthSec)
{
var originalClipSamples = new float[originalClip.samples];
originalClip.GetData(originalClipSamples, 0);
//converts startPosSec & takeAmountSec from seconds to sample amount
int newStartPosSample = (int)(startPosSec * originalClip.frequency);
int newLengthSecSample = (int)(lengthSec * originalClip.frequency);
//gets the trimmed version of the orignalClipSamples
var newClipSamples = originalClipSamples.Skip(newStartPosSample).Take(newLengthSecSample).ToArray();
//generates a new empty clip and sets its data according to the newClipSamples
AudioClip resClip = AudioClip.Create(originalClip.name, newClipSamples.Length, originalClip.channels, originalClip.frequency, false);
resClip.SetData(newClipSamples, 0);
return resClip;
}
//is called on the update function
public void UpdateRecorder()
{
lock (locker)
{
if (record)
{
if (!isRecording)
{
recording = Microphone.Start("", false, currRecordingLen, 44100);
startTime = DateTime.Now;
isRecording = true;
}
}
else if (isRecording)
{
Microphone.End("");
if (currRecordingPath != "")
{
//trims the clip by the amount of seconds that have passed
if (verbose != null)
{
float[] samples = new float[recording.samples];
recording.GetData(samples, 0);
verbose.text += "avrg: " + samples.Average().ToString() + '\n';
}
AudioClip newClip = TrimAudioClip(recording, 0, (float)(DateTime.Now - startTime).TotalSeconds);
if (verbose != null)
{
float[] samples = new float[recording.samples];
newClip.GetData(samples, 0);
verbose.text += "avrg: " + samples.Average().ToString() + '\n';
}
//saves the clip in the given path
SavWav.Save(currRecordingPath, newClip);
isRecording = false;
}
}
}
}
}