Search Unity

Audio GetComponent<AudioSource>() not working + Microphone.devices is empty.

Discussion in 'Audio & Video' started by TheDJPHD, Jul 22, 2019.

  1. TheDJPHD

    TheDJPHD

    Joined:
    Jul 12, 2019
    Posts:
    1
    I'm trying to make voice recognition work in my unity game. To do so, I need to use a microphone to record a 1 second command when I press a key (let's say V).

    I start off with:
    private AudioSource mic;

    I then get the AudioSource component, and then try and get the names of all available microphones, where microfone is a string.

    mic.GetComponent<AudioSource>();
    foreach (string device in Microphone.devices)
    {
    //setting the first device as the default.
    if (microfone == null)
    {
    microfone = device;
    }
    options.Add(device);
    }

    Unfortunately, the for loop never iterates, meaning that Microphone.devices is empty.
    I also believe that mic.getComponent isn't working either.

    When V is pressed I call a function. It works down until it hits mic.Stop().
    I use mic.Stop() to ensure that it isn't already recording when I start it again.

    Once V is pressed, it attempts to execute 'mic.Stop()'. The game then freezes (it's in some sort of infinite loop?)

    In Edit-->Preferences-->Audio I've made sure that the Disable Audio box is unchecked. Is there a reason that anyone knows that Unity isn't seeing the available devices? If anyone knows, please let me know.

    I'll be digging in the meantime :)

    Thanks,
    JP