首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

在.net中用c#开发程序,播放声音有关问题,请知道赐教

2012-01-09 
在.net中用c#开发程序,播放声音问题,请知道赐教!我的播放声音的代码如下:[System.Flags]publicenumPlaySou

在.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

添加资源
添加现有文件


[解决办法]
try..

private void playSoundFromResource()
{
SoundPlayer sndPing = new SoundPlayer(SoundRes.GetType(), "Ping.wav ");
sndPing.Play();
}
[解决办法]
我的调试环境是2005
你可以参考liujia_0421的代码 //MSDN里Copy出来的
[解决办法]
TO:我的调试环境是2005
你可以参考liujia_0421的代码 //MSDN里Copy出来的

呵呵,被你看出来了...
[解决办法]
呵呵,我也拷贝了那段代码
但发现编译通不过
所以改了改,变量名都没变哈
[解决办法]
我在2003上调试,通过了.
代码如下:

[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);
public const int SND_FILENAME = 0x00020000;
public const int SND_ASYNC = 0x0001;


private void button1_Click(object sender, System.EventArgs e)
{
try
{
PlaySound( "\\My Documents\\train.wav ", new System.IntPtr(), PlaySoundFlags.SND_SYNC);
}
catch(System.Exception ex)
{
MessageBox.Show(ex.Message, "Error Message ");
}

热点排行