0

I want to play a video but before the video is played i want to know if the audio device connected to the system is working.

  public static bool IsAudioDeviceAvailable()
    {
        ManagementObjectSearcher objSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_SoundDevice");
        ManagementObjectCollection objCollection = objSearcher.Get();

        if(objCollection.Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

but it doesnt work

Solution I used suggested by user3060520

Detecting Audio Input & output devices connected to system

  public static bool IsAudioDeviceAvailable()
    {
        string[] mydevices = null;

        mydevices = Win32.GetSoundDevices();
        if (mydevices.Count() > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}


public class Win32
{
    [DllImport("winmm.dll", SetLastError = true)]
    static extern uint waveOutGetNumDevs();

    [DllImport("winmm.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern uint waveOutGetDevCaps(uint hwo, ref WAVEOUTCAPS pwoc, uint cbwoc);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct WAVEOUTCAPS
    {
        public ushort wMid;
        public ushort wPid;
        public uint vDriverVersion;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string szPname;
        public uint dwFormats;
        public ushort wChannels;
        public ushort wReserved1;
        public uint dwSupport;
    }

    public static string[] GetSoundDevices()
    {
        uint devices = waveOutGetNumDevs();
        string[] result = new string[devices];
        WAVEOUTCAPS caps = new WAVEOUTCAPS();

        for (uint i = 0; i < devices; i++)
        {
            waveOutGetDevCaps(i, ref caps, (uint)Marshal.SizeOf(caps));
            result[i] = caps.szPname;
        }
        return result;
    }

Also i would like to add that it will not work for virtual audio devices.

jww
  • 97,681
  • 90
  • 411
  • 885
Quest
  • 125
  • 8
  • Please check: [Detecting Audio Input & output devices connected to system](http://stackoverflow.com/questions/5936434/detecting-audio-input-output-devices-connected-to-system) – huse.ckr Oct 21 '16 at 10:52
  • 1
    You need to look at the Availability and/or Status and/or StatusInfo properties of Win32_SoundDevice. – stuartd Oct 21 '16 at 10:58
  • @Quest, I've added an answer. See if it works for you. – Kamalesh Wankhede Oct 21 '16 at 11:31
  • I think you found a solution to your problem, but your question makes that unclear. An actual answer/solution should **not** be edited into your Question. In general, you should [edit] the Question to *clarify the Question*, but not to include an Answer within the Question. You should create your own Answer with the code you used to solve your problem, then accept it (the system may require a 48 hour delay prior to accepting your own answer). When you have solved the problem yourself, [answering your own question is encouraged](//stackoverflow.com/help/self-answer). – Makyen Oct 14 '18 at 23:57

1 Answers1

0

You can also use devcon.exe to find devices by ID / Class etc and parse its status. Eg.

devcon.exe status =media         //this will find all media devices and show its tatus
devcon.exe status [Device HW ID] //this will find device by id and show its status

sample output:

devcon.exe status [Device HW ID]

Device HW ID
    Name: Realtek High Definition Audio
    Driver is running.

Now you only have to parse the output string looking for "Driver is running" or smthing like that do detect wheter device driver is working correctly. You can find more info about devcon here

Zwierzak
  • 666
  • 1
  • 11
  • 31
  • user3060520 answer was the one i used and it works. Thanks very much. I have updated my question with the code. – Quest Oct 21 '16 at 15:09