Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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
visual c#2010以表格1播放声音(来自resorce文件)_C#_.net_Forms_Audio - Fatal编程技术网

visual c#2010以表格1播放声音(来自resorce文件)

visual c#2010以表格1播放声音(来自resorce文件),c#,.net,forms,audio,C#,.net,Forms,Audio,好的,我正在尝试让我的表格1应用程序播放一个.wav文件,我已经导入到我的资源文件中,我已经在线下载了这个文件,但我能找到的只是 Sub PlayBackgroundSoundFile() My.Computer.Audio.Play("C:\Waterfall.wav", _ AudioPlayMode.Background) End Sub 但我相信这是针对控制台应用程序的,如果不考虑我把代码放在哪里,我会遇到很多错误 以下是我正在使用的代码: using Syst

好的,我正在尝试让我的表格1应用程序播放一个.wav文件,我已经导入到我的资源文件中,我已经在线下载了这个文件,但我能找到的只是

Sub PlayBackgroundSoundFile()
    My.Computer.Audio.Play("C:\Waterfall.wav", _
        AudioPlayMode.Background) End Sub
但我相信这是针对控制台应用程序的,如果不考虑我把代码放在哪里,我会遇到很多错误

以下是我正在使用的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            var myForm = new Form1();
            myForm.Show();




        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

        }
        private class form1_load
        {

        }
    }
}
我还需要补充什么?我以前从未使用过这样的方法
如果您只需要编辑代码,则文件名为sound.wav

我只是自己做的:使用一个按钮创建简单表单,并为此按钮创建事件。Form1课程的内容如下:

    using System;
    using System.Media;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void btPlay_Click(object sender, EventArgs e)
            {
                string path;
                using (var dlg = new OpenFileDialog())
                {
                    dlg.Multiselect = false;
                    dlg.Filter = "WAV files|*.wav";
                    if (dlg.ShowDialog() == DialogResult.Cancel) return;
                    path = dlg.FileName;
                }
                SoundPlayer sp = new SoundPlayer(path);
                sp.Play();
            }  
        }
    }

而且它工作得很好。试试这个,如果你仍然会有错误,你可以在这里张贴正确的错误。这将有助于我帮助您。

请尝试“无错误”,它希望我生成一半的代码顺便说一句,据我所知,您找到的是Visual Basic中的代码。感谢您的帮助,我找到了一个解决方案“SoundPlayer=新SoundPlayer(Properties.Resources.sound)”;player.Play()'