日期:2014-05-20  浏览次数:20890 次

在.net中用c#开发程序,播放声音问题,请知道赐教!
我的播放声音的代码如下:
                        [System.Flags]
                        public   enum   PlaySoundFlags   :   int
                        {
                                SND_SYNC   =   0x0000,
                                SND_ASYNC   =   0x0001,
                                SND_NODEFAULT   =   0x0002,
                                SND_LOOP   =   0x0008,
                                SND_NOSTOP   =   0x0010,
                                SND_NOWAIT   =   0x00002000,
                                SND_FILENAME   =   0x00020000,
                                SND_RESOURCE   =   0x00040004
                        }
                [System.Runtime.InteropServices.DllImport( "CoreDll.DLL ",   EntryPoint   =   "PlaySound ",   SetLastError   =   true)]
                        private   static   extern   bool   PlaySound(string   szSound,   System.IntPtr   hMod,   PlaySoundFlags   flags);
                private   void   PlaySuccess()
                {
                        PlaySound( "bbb.wav ",   new   System.IntPtr(),   PlaySoundFlags.SND_SYNC);
                }
                private   void   PlayUnsuccess()
                {
                        PlaySound(   "aaa.wav ",   new   System.IntPtr(),   PlaySoundFlags.SND_SYNC);
                }

问题是,如何只用文件名就能够播放,我现在必须要加入全路径才能播放。这个问题是不是和资源有关系?如果有关系的话,怎么添加编译,调用资源?

------解决方案--------------------
//有标准类就用标类,尽量别用API
//参考如下代码
private void button1_Click(object sender, EventArgs e)
{
SoundPlayer sndPing = new SoundPlayer(); // using System.Media;
sndPing.Stream = Properties.Resources.MySound;
sndPing.Play();
}
------解决方案--------------------
打开

解决方案
ApplicationName
Properties
Resources.resx

添加资源
添加现有文件