Sirithang
Sirithang

Reputation: 155

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#?

Upvotes: 7

Views: 1320

Answers (2)

Sivodaya Tech
Sivodaya Tech

Reputation: 128

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.");
    }
}

Upvotes: 0

Andrew Russell
Andrew Russell

Reputation: 27215

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.

Upvotes: 6

Related Questions