Sound Recording and playing Issue

Hello All,

I am trying to record and play a sound in my app using Microphone class of unity.
but it is not working for me.here is my code :

using UnityEngine; //41 Post - Created by DimasTheDriver on July/28/2012 . Part of the 'Unity: Capturing audio from a microphone' post. Available at: http://www.41post.com/?p=4884
using System.Collections;

[RequireComponent (typeof (AudioSource))]

public class SingleMicrophoneCapture : MonoBehaviour 
{
	//A boolean that flags whether there's a connected microphone
	private bool micConnected = false;
	
	//The maximum and minimum available recording frequencies
	private int minFreq = 0;
	private int maxFreq = 0;
	private string device_Name;
	
	//A handle to the attached AudioSource
	private AudioSource goAudioSource;
	
	//Use this for initialization
	void Start() 
	{
		//Check if there is at least one microphone connected
		if(Microphone.devices.Length <= 0)
		{
			//Throw a warning message at the console if there isn't
		}
		else //At least one microphone is present
		{
			//Set 'micConnected' to true
			micConnected = true;
			
			foreach (string device in Microphone.devices) 
			{
            	device_Name = device[0].ToString();
        	}
			//Get the default microphone recording capabilities
			Microphone.GetDeviceCaps(null, out minFreq, out maxFreq);
			
			//According to the documentation, if minFreq and maxFreq are zero, the microphone supports any frequency...
			if(minFreq == 0  maxFreq == 0)
			{
				//...meaning 44100 Hz can be used as the recording sampling rate
				maxFreq = 44100;
			}
			
			//Get the attached AudioSource component
			goAudioSource = this.GetComponent<AudioSource>();
		}
	}
	
	void OnGUI() 
	{
		//If there is a microphone
		if(micConnected)
		{
			//If the audio from any microphone isn't being recorded
			if(!Microphone.IsRecording(device_Name))
			{
				//Case the 'Record' button gets pressed
				if(GUI.Button(new Rect(Screen.width/2-100, Screen.height/2-25, 200, 50), "Record"))
				{
					//Start recording and store the audio captured from the microphone at the AudioClip in the AudioSource
					goAudioSource.clip = Microphone.Start(device_Name, true, 20, maxFreq);
				}
			}
			else //Recording is in progress
			{
				//Case the 'Stop and Play' button gets pressed
				if(GUI.Button(new Rect(Screen.width/2-100, Screen.height/2-25, 200, 50), "Stop and Play!"))
				{
					Debug.Log("in OnGUi() -> if(micConnected -> if(!Microphone.IsRecording) -> else part -> Stop  Play Button");
					Microphone.End(device_Name); //Stop the audio recording
					goAudioSource.Play(); //Playback the recorded audio
				}
				
				GUI.Label(new Rect(Screen.width/2-100, Screen.height/2+25, 200, 50), "Recording in progress...");
			}
		}
		else // No microphone
		{
			GUI.contentColor = Color.red;
			GUI.Label(new Rect(Screen.width/2-100, Screen.height/2-25, 200, 50), "Microphone not connected!");
		}
	}
}

I don’t know whats wrong that i am doing here in my code…
Also i am trying to find the path of my sound file that i am recording here with my code,but that also i am not able to find.
can any one help me for this…???

Thanks in Advance!
Niki.j

Hello All,

i have done something,but still i can not hear the sound…
here is the sample,please let me know if anyone can help me regarding this…

Thanks for the help!
Niki.j
Skype : nitinjindal1986

1235830–52429–$CapturingMicInput.zip (568 KB)

Hello Guys…

Finally got everything working fine…
here is my code…

//=====
//Created By Nitin Jindal
//Date : 03/05/2013
//Audio Recording and playing
//====

using UnityEngine; 
using System.Collections;

[RequireComponent (typeof (AudioSource))]

public class SingleMicrophoneCapture : MonoBehaviour 
{
	private bool micConnected = false;
	
	private int minFreq;
	private int maxFreq;
	
	private AudioSource goAudioSource;
	
	void Start() 
	{
		if(Microphone.devices.Length <= 0)
		{
			//Do Nothing Because Microphone is not connected
		}
		else
		{
			micConnected = true;
			
			Microphone.GetDeviceCaps(null, out minFreq, out maxFreq);
			
			if(minFreq == 0  maxFreq == 0)
			{
				maxFreq = 44100;
			}
			
			goAudioSource = this.GetComponent<AudioSource>();
		}
	}
	
	void OnGUI() 
	{
		if(micConnected)
		{
			if(!Microphone.IsRecording(null))
			{
				if(GUI.Button(new Rect(Screen.width/2-100, Screen.height/2-25, 200, 50), "Record"))
				{
					goAudioSource.clip = Microphone.Start(null, true, 20, maxFreq);
				}
			}
			else
			{
				if(GUI.Button(new Rect(Screen.width/2-100, Screen.height/2-75, 200, 50), "Stop  Play!"))
				{
					Microphone.End(null); 
					goAudioSource.Play(); 
				}
			}
		}
	}
}


Enjoy guys....

Niki.j

Do any of you that tried to record on Android or IOS get an annoying noise with your recording? I tried to low the volume but no AudioSource parameter seem to make any difference. Any clues?

Thank you!