I'm having problem with sound file(*.wav)

In a form, when i click a button, i want to call sound file and play this file.

How can i do?

Help me!~~~

Dani AI

Generated

— the simplest, most reliable approach for a WinForms button click is the built‑in System.Media.SoundPlayer (this is exactly what was suggesting). SoundPlayer is intended for playing .wav files and can load from a file path, a URL or a Stream; it exposes Play, PlaySync, PlayLooping and Load/LoadAsync for simple async/sync playback. It will not play MP3/WMA — for compressed formats a different API is needed. (learn.microsoft.com)

Common reasons a .wav “doesn’t play” and how to check them:

  • The process cannot find the file (absolute path vs. working directory). Verify the exact path and test with a hard path first.
  • The file isn’t being copied into the app output: mark the WAV in the project as Content (or Embedded Resource) and set “Copy to Output Directory” to “Copy if newer” / “Copy always” if using a relative path.
  • The WAV uses an unusual codec (system codecs matter) or playback throws an exception — catch and inspect ArgumentException / DirectoryNotFoundException / IOException to see the cause. (learn.microsoft.com)

When WAV-only SoundPlayer isn’t enough:

  • For MP3 and multimedia features, use a media engine (WPF’s MediaPlayer or the Windows Media Player ActiveX control in WinForms). (learn.microsoft.com)
  • For low-latency, mixing, format support (MP3/WMA/AAC) or programmatic control, consider NAudio (NuGet/GitHub) which supports many formats and APIs. (github.com)
  • A compact Win32 fallback for WAVs is the PlaySound API via P/Invoke (good for tiny utilities): (learn.microsoft.com)
using System;
using System.Runtime.InteropServices;

[DllImport("winmm.dll", SetLastError=true)]
static extern bool PlaySound(string pszSound, IntPtr hmod, uint fdwSound);

const uint SND_FILENAME = 0x00020000;
const uint SND_ASYNC    = 0x0001;

// inside button handler:
PlaySound(@"C:\path\to\sound.wav", IntPtr.Zero, SND_FILENAME | SND_ASYNC);

Practical checklist to resolve the original problem: confirm the file is a standard .wav, test with an absolute path, set the file to copy to output or embed it (SoundPlayer can load from a Stream), prefer SoundPlayer.Play() (async) to avoid UI blocking, and switch to MediaPlayer/NAudio only if MP3 or advanced control is required. Embedding avoids path issues altogether (see the embedded‑resource example in the WinForms docs). (learn.microsoft.com)

Finally, ’s CodeGuru pointer was along the right lines historically, but the current Microsoft docs and the options above give a concise, reliable set of choices for modern projects.

Recommended Answers

All 2 Replies

ever heard something about googling ? :evil:

System.Media.SoundPlayer p = new System.Media.SoundPlayer();
p.SoundLocation = @"C:\Users\Master\Desktop\Prmpt445.wav";
p.Play();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.