你如何在C#中播放声音?

我试过这个但是我很感兴趣从我的程序开始播放声音。 这样我在项目文件夹中有.wav文件。

SoundPlayer simpleSound = new SoundPlayer(@"/yay.wav"); simpleSound.Play(); 

谢谢

这样我在项目文件夹中有.wav文件。

这可能是你的问题。

编译应用程序时,它不会直接在项目文件夹中结束 – 它最终位于子目录( /Debug/bin/Release/bin )中。 将wav文件放在那里而不是项目目录中,看看它是如何工作的。

在播放声音之前,您必须熟悉PlaySound()Win32 API函数。

 private SoundPlayer player = new SoundPlayer(); /// Button click event handler. private void AsyncBtn_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { // Set .wav file as TextBox.Text. textBox1.Text = openFileDialog1.FileName; // Add LoadCompleted event handler. player.LoadCompleted += new AsyncCompletedEventHandler(LoadCompleted); // Set location of the .wav file. player.SoundLocation = openFileDialog1.FileName; // Load asynchronously. player.LoadAsync(); } } /// LoadCompleted event handler. private void LoadCompleted(object sender, AsyncCompletedEventArgs args) { player.Play(); }