Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c语言中播放背景音#_C#_Audio - Fatal编程技术网

C# 如何在c语言中播放背景音#

C# 如何在c语言中播放背景音#,c#,audio,C#,Audio,我已经学习了在c#中添加声音的教程,但是它不能播放声音,但是没有错误。我该怎么办 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用System.Windows.Forms; 使用系统、媒体; 命名空间Windows窗体应用程序2 { 公共部分类Form1:Form { 公共表格1() { 初始化组件(); } 私有void Form1\u加载(对象发送方、事件参数e) {

我已经学习了在c#中添加声音的教程,但是它不能播放声音,但是没有错误。我该怎么办

使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用系统、媒体;
命名空间Windows窗体应用程序2
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
SoundPlayer myPlayer=新的SoundPlayer(WindowsFormsApplication2.Properties.Resources.sound1);
myPlayer.PlayLooping();
}
}
}

确保已将
Form1\u Load()
分配给表单
Load
事件。我已经输入了你的代码,效果很好

要分配事件,只需查看下图

这将确保在加载表单时调用
Form1\u Load()

在测试了尝试将非.WAV文件传递给
SoundPlayer
时会发生什么情况后,它将产生错误:

错误1“System.Media.SoundPlayer.SoundPlayer(string)”的最佳重载方法匹配有一些无效参数


这让我相信代码没有被调用。

SoundPlayer仅适用于WAV文件。您试图加载的文件是否为WAV文件?是否已添加该文件?是否sound@user2187491问题是,它是WAV文件吗?您知道,WAV文件格式。我在资源中添加了sound.WAV,它没有错误,但没有声音。是否调用了
Form1\u Load(…)
?也许你忘了添加事件。一个友好的提示:一般来说,在回答问题之前,最好确保你理解了确切的问题。有时候你会很幸运,但有时候就不那么幸运了。