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.