Hi, i am trying to record from microphone to a file. Specifically i want the user to press a button, which than starts the recording until another button is pressed which ends the recording process. Unforunately i was unable to find a solution for this simple task. The Microphone.Start() uses a predetermined time. I found a Asset called G-Audio which can do just this. (Example 5, free Asset) But it is so complicated and not well deocumented that i can not extract this functionality. Is there anyone who can help with this problem? I am Googling for 2 Days, it seems as if noone could solve this problem or noone posted a solution to this.
Here's the answer I posted to the short lived new Unity forum! As I mentioned there, it stills needs some kind of fading so that the loops don't click each time they repeat themselves. Start a recording with the Space bar, and toggle playback of loops using the numbers keys, the first loop being number 1. Code (CSharp): using UnityEngine; using System.Collections.Generic; public class CubeController : MonoBehaviour { bool isRecording = true; private AudioSource audioSource; //temporary audio vector we write to every second while recording is enabled.. List<float> tempRecording = new List<float>(); //list of recorded clips... List<float[]> recordedClips = new List<float[]>(); void Start() { audioSource = GetComponent<AudioSource>(); //set up recording to last a max of 1 seconds and loop over and over audioSource.clip = Microphone.Start(null, true, 1, 44100); audioSource.Play(); //resize our temporary vector every second Invoke("ResizeRecording", 1); } void ResizeRecording() { if (isRecording) { //add the next second of recorded audio to temp vector int length = 44100; float[] clipData = new float[length]; audioSource.clip.GetData(clipData, 0); tempRecording.AddRange(clipData); Invoke("ResizeRecording", 1); } } void Update() { //space key triggers recording to start... if (Input.GetKeyDown("space")) { isRecording = !isRecording; Debug.Log(isRecording == true ? "Is Recording" : "Off"); if (isRecording == false) { //stop recording, get length, create a new array of samples int length = Microphone.GetPosition(null); Microphone.End(null); float[] clipData = new float[length]; audioSource.clip.GetData(clipData, 0); //create a larger vector that will have enough space to hold our temporary //recording, and the last section of the current recording float[] fullClip = new float[clipData.Length + tempRecording.Count]; for (int i = 0; i < fullClip.Length; i++) { //write data all recorded data to fullCLip vector if (i < tempRecording.Count) fullClip[i] = tempRecording[i]; else fullClip[i] = clipData[i - tempRecording.Count]; } recordedClips.Add(fullClip); audioSource.clip = AudioClip.Create("recorded samples", fullClip.Length, 1, 44100, false); audioSource.clip.SetData(fullClip, 0); audioSource.loop = true; } else { //stop audio playback and start new recording... audioSource.Stop(); tempRecording.Clear(); Microphone.End(null); audioSource.clip = Microphone.Start(null, true, 1, 44100); Invoke("ResizeRecording", 1); } } //use number keys to switch between recorded clips, start from 1!! for (int i = 0; i < 10; i++) { if (Input.GetKeyDown("" + i)) { SwitchClips(i-1); } } } //chooose which clip to play based on number key.. void SwitchClips(int index) { if (index < recordedClips.Count) { audioSource.Stop(); int length = recordedClips[index].Length; audioSource.clip = AudioClip.Create("recorded samples", length, 1, 44100, false); audioSource.clip.SetData(recordedClips[index], 0); audioSource.loop = true; audioSource.Play(); } } }
ok this is off topic but what would be the way to convert this to something that could be send across a network. So say you wanted to have live chat in a multiplayer game.
Hi @jbsenthilkumar209 , I put the code on my GameObject with an AudioSource. The space bar is working and I have an audio clip but... The audio clip is empty (cf. screenshots). My microphone is working on Windows (I am using the built-in Realtek audio device of the motherboard). How did you succeed to use this script ? Thanks a lot in advance ! (@rorywalsh)
Oups, sory, EDIT --> This is working but the sound is very irregular and interspersed with silences , Any ideas ?!