我已经尝试了这个但是我很感兴趣从我的程序开始播放声音。这样我在项目文件夹中有.wav文件。
SoundPlayer simpleSound = new SoundPlayer(@"/yay.wav");
simpleSound.Play();
谢谢
答案 0 :(得分:3)
这样我在项目文件夹中有.wav文件。
这可能是你的问题。
编译应用程序时,它不会直接在项目文件夹中结束 - 它最终位于子目录(/Debug/bin
或/Release/bin
)中。将wav文件放在那里,而不是放在项目目录中,看看它是如何工作的。
答案 1 :(得分:1)
在播放声音之前,您必须熟悉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();
}