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!~~~
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!~~~
— 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:
When WAV-only SoundPlayer isn’t enough:
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.
Jump to Post— r0ckbaer 3ever heard something about googling ? :evil:
ever heard something about googling ? :evil:
System.Media.SoundPlayer p = new System.Media.SoundPlayer();
p.SoundLocation = @"C:\Users\Master\Desktop\Prmpt445.wav";
p.Play();
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.