Home > Enterprise >  How can you make a microphone toggle with unity?
How can you make a microphone toggle with unity?

Time:09-27

I had a question regarding microphone toggles. I've been searching online for a solid way to make a mic toggle, and couldn't find it. Just wanted to ask if something like this would do the job:

string _deviceName = Microphone.devices[0];

if (Input.GetKeyDown(KeyCode.M) {
micIsOn = !micIsOn;
}

if (micIsOn) {
Microphone.End(_deviceName);
}

else {
Microphone.Start(_deviceName, true, 10, AudioSettings.outputSampleRate;
}

CodePudding user response:

If his is being called in update, which I think it is, you should put the rest of the if statements in the first one. This way, it will update when you press the button and not every frame. You are currently keep starting a new microphon every frame.

string _deviceName = Microphone.devices[0];

if (Input.GetKeyDown(KeyCode.M) {
    micIsOn = !micIsOn;
    if (micIsOn) {
        Microphone.End(_deviceName);
    }

    else {
    Microphone.Start(_deviceName, true, 10, AudioSettings.outputSampleRate);
     //also you need the ")"
    }
}
  • Related