Make sure the system has C # sound

We are currently developing a game using XNA, and we came across a small problem with the sound.

If the system does not have a connected audio device (speakers, etc.), when Win7 shows a red cross on the speaker icon), it fails when trying to play / download sound.

So, we would like to check if the system has the ability to output sound. Is this possible in C #?

+6
source share
2 answers

Are you sure this is really a failure, not just throwing an unhandled exception?

In theory, it should throw a NoAudioHardwareException .

Try to do something with sound ( SoundEffect.MasterVolume comes to mind as an opportunity, as it is a static method) and see if you can catch the exception. If you catch an exception, just don't do further sound work.

+6
source

I think this will help .........

 [DllImport("winmm.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] public static extern long GetNumDevs(); private void Button1_Click(System.Object sender, System.EventArgs e) { long I = 0; I = GetNumDevs(); if (I > 0) { Interaction.MsgBox("Your system can play sound files."); } else { Interaction.MsgBox("Your system can not play sound files."); } } 
0
source

Source: https://habr.com/ru/post/885819/


All Articles