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