Recording and Saving an AudioClip from a Microphone

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
User avatar
Joel
Moodkie Staff
Posts: 4810
Joined: Wed Nov 07, 2012 10:32 pm

Recording and Saving an AudioClip from a Microphone

Post by Joel »

Recording and Saving an AudioClip from a Microphone

In this example we will be creating a simple Audio Recorder, which takes audio from a microphone and then saves it so we can play it back at a later time.

How it Works

If audio has been previously been recorded, a button will be displayed to allow you to play back that audio.

There will also be buttons which allow you to select an audio device to record from. Upon clicking one of these, a Record button will be displayed. Pressing record will start recording from this microphone, until the Stop button is pressed. This audio is then saved so it can be played back later, overwriting the old audio data.

Requirements
  • A microphone connected to and recognised by your device.
  • A basic knowledge of C#.
  • Note that if using Web Player, you are limited to 1MB of save data.
1. The AudioRecorder script and it's Instance Variables

First, we must create our AudioRecorder script and create some instance variables.
  • microphoneName : Stores the name of the microphone we want to record from. This will be a blank string to start off with.
  • saveFile : The name of the file we want to create to store our audio clip.
  • recordedAudio : An AudioSource Component which we will create in the Start() function to allow us to play our recorded audio.
With these created, the AudioRecorder script looks like this:

C#
using UnityEngine;
using System.Collections;

public class AudioRecorder : MonoBehaviour 
{
   private string microphoneName = "";
   private string saveFile = "myAudio.txt";
   private AudioSource recordedAudio;
}
2. The Start() method

First, we must create our AudioSource using AddComponent.

If we're using Web Player, we must also request authorisation from the user before we're allowed to record from the Microphone. To do this, we turn our Start() method into an IEnumerator Start() method. This allows us to use the Application.RequestUserAuthorization() function, which automatically asks the user for permission.

Note: Using Application.RequestUserAuthorization on any other platform automatically grants permission to use the microphone.

The Start() function should look like this:

C#
IEnumerator Start () 
{
   recordedAudio = gameObject.AddComponent<AudioSource>();
   yield return Application.RequestUserAuthorization(UserAuthorization.Microphone);
}
3. The OnGUI() method

We will use the OnGUI() method to display the correct buttons at the correct time.

First, we must check that authorisation to use the microphone has been granted. If it has been granted, we do one of two things:
  • If our microphoneName variable IS blank, we need to select a microphone to record from, so we should display a list of microphones.
  • If our microphone variable IS NOT blank, we should show a record button, allowing the user to record from this microphone.
In our case, we either call the ShowMicrophoneList() or the ShowRecordButton() function, both of which we will create later.

Another thing we must do in OnGUI() is to check if there is a previously saved recording which we can load and play. We check this using ES2.Exists() and providing our saveFile variable as a parameter. If it does exist, we call a ShowLoadButton() function, which we will create later on.

The OnGUI() function should look like this:

C#
private void OnGUI() 
{		
   if(Application.HasUserAuthorization(UserAuthorization.Microphone))
   {
      if(microphoneName == "")
         ShowMicrophoneList();
      else
         ShowRecordButton();
   }
	
   if(ES2.Exists(saveFile))
      ShowLoadButton();
}
4. The ShowMicrophoneList() method

The ShowMicrophoneList() method will display a list of microphones. When the user chooses one, the name of the microphone will be stored in our microphoneName variable.

We use a foreach statement to loop over the list of microphones held in Unity's Microphone.devices array, and create a button for each. Clicking a button assigns the name of that microphone to our microphoneName variable.

Note: We create a deviceNo variable to help us display the buttons one after the other, but you can do this whatever way you feel comfortable with.

The ShowMicrophoneList() function should look similar to this:

C#
private void ShowMicrophoneList()
{
   int deviceNo = 0;
   foreach(string device in Microphone.devices)
   {	
      if(GUI.Button(new Rect(0, 50*deviceNo, Screen.width/2, 50), device))
         microphoneName = device;

      deviceNo++;
   }
}
5. The ShowRecordButton() method

If the current microphone is not currently recording, the ShowRecordButton() function will show a Start Recording button. Otherwise, it will show a Stop Recordng button. We check if a microphone is recording using the Microphone.isRecording() method, and use our microphoneName variable as a parameter.

When pressed, the Start Recording button calls the Microphone.Start() method with our microphoneName, and assigns the AudioClip it creates to the clip variable contained in our recordedAudio variable. For more information on what each of the parameters mean, see the Microphone.Start() documentation.

When the Stop Recording button is pressed, we call Microphone.End() method to stop recording, and then we save the AudioClip using ES2.Save(), supplying the recordedAudio.clip and saveFile variables as parameters. Finally, we play the AudioClip.

The ShowRecordButton() function should look similar to this:

C#
private void ShowRecordButton()
{
   if(!Microphone.IsRecording(microphoneName))
   {
      if(GUI.Button(new Rect(0, 0, Screen.width/2, 50), "Start Recording"))
         recordedAudio.clip = Microphone.Start(microphoneName, true, 10, 44100);
   }
   else
   {
      if(GUI.Button(new Rect(0, 0, Screen.width/2, 50), "Stop Recording"))
      {
         Microphone.End(microphoneName);
         ES2.Save(recordedAudio.clip, saveFile);
         recordedAudio.Play();
      }
   }
}
5. The ShowLoadButton() method

The ShowLoadButton() method is called if there is a previous recording that we can load and play.

It displays a Play Previous Recording button which, when pressed, uses ES2.Load<AudioClip>() to load the audio data held in our save file, assign it to the recordedAudio.clip variable and then play it.

The ShowLoadButton() method should look like this:

C#
private void ShowLoadButton()
{
   if(GUI.Button(new Rect(Screen.width/2, 0, Screen.width/2, 50), "Play Previous Recording"))
   {
      recordedAudio.clip = ES2.Load<AudioClip>(saveFile);
      recordedAudio.Play();
   }
}
Now you are finished, simply attach the script to any GameObject in your scene to see it working.
Locked