Check if system has sound C#
We are currently developing a game using XNA and we've stumbled upon a little sound problem.
When a system has no sound 开发者_运维问答device plugged in (speakers, etc. -- when Win7 shows a red cross on the speaker icon) it crashes when trying to play/load the sound.
So, we would like to check if the system has the capacity of outputting sound. Is it possible in C#?
Are you sure that it is actually crashing, and not simply throwing an unhandled exception?
In theory it should throw a NoAudioHardwareException
.
Try doing something with audio (SoundEffect.MasterVolume
comes to mind as a possibility, as it is a static method) and see if you can catch the exception. If you do catch an exception, simply do no further audio work.
i think this will helps.........
[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.");
}
}
精彩评论