How to use Microphone.Start/End for clips of unknown length?

I’m trying to add functionality that allows the user to record an audio clip, then run whatever analysis I’ve written on the audio once they’ve finished. The basic implementation seems obvious:

public AudioClip myClip;

void Update(){
	if (Input.GetKey (KeyCode.P)) {
		myClip = Microphone.Start(null, false, 10, samplerate);			//Start recording (rewriting older recordings)
	if (Input.GetKey (KeyCode.O)) {										//Stop recording
		MyAnalyzer(myClip);           //Run whatever analysis I want on the recording

The problem is that Microphone.Start requires me to pass it a clip length when I call the method, and I would ideally like to let the user decide the recording length. The hacky solution is to leave the length at something far longer than the use case is likely to demand, but that’s ugly and wastes memory. Is there a simple way to leave the length unspecified when I start recording?

For asking the user the recording length beforehand:

Ask the user to input the clip length. Add a field to hold the clip length and edit the code that runs after the user presses “P”.

using System; //necessary for conversion of string to integer

private int clipLength; //number of seconds of the audio clip length

        if (Input.GetKey(KeyCode.P))
            //Ask the user to enter the length of the clip.  A string will be returned
            //that will be later converted to integer.
            //The last parameter is the length of the string the user can input.
            //So a value of 3 corresponds to a maximum valid number of 999 seconds.  Adjust this as necessary.
            //Also adjust the default to be offered to the user for editing (now "10")
            string stringInput = GUI.TextField(new Rect(10, 10, 200, 20), "10", 3);

            //Try to convert the input text to an integer. If the conversion is successful
            //the function will return "true"
            bool conversionSuccessful = Int32.TryParse(stringInput, out clipLength);

            if (conversionSuccessful)
                myClip = Microphone.Start(null, false, clipLength, samplerate); //Start recording (rewriting older recordings)
            //    //optionally output a suitable message to the user


For determining the recording length at run time, and trimming a temporary audio clip to its non-empty part:

  1. Use a temporary AudioClip “tempClip”, to start the recording with Microphone.Start(). Say 60 seconds length, or whatever length you like. A maximum length for the temp clip would also be useful in controlling the use of system resources too.
  2. Have the user press a key to stop the recording, say “S”.
  3. Before ending the recording call Microphone.GetPosition(). This will return an int with the the position in samples of the recording, say we call it “lastSample”. You can use this to trim the useful, non-empty part of tempClip.
  4. Use AudioClip.GetData() and AudioClip.SetData() in conjucntion with lastSample to store the useful part of tempClip in myClip for further analysis.

I hope this helps.


public void StopRecord()
int lastTime = Microphone.GetPosition(null);
if (lastTime == 0)

    Debuger.Log("lastTime =" + lastTime);
    float[] samples = new float[AudioSource.clip.samples]; //
    AudioSource.clip.GetData(samples, 0);
    float[] ClipSamples = new float[lastTime];
    Array.Copy(samples, ClipSamples, ClipSamples.Length - 1);
    AudioSource.clip = AudioClip.Create("playRecordClip", ClipSamples.Length, 1, 44100, false, false);
    AudioSource.clip.SetData(ClipSamples, 0);