
C#winform 中,对每个按钮添加音乐,但是比较麻烦,能把他做成类,直接调用吗?
System.Media.SoundPlayerp1=newSystem.Media.SoundPlayer();p1.SoundLocation=@"E:\Smartz...
System.Media.SoundPlayer p1 = new System.Media.SoundPlayer();
p1.SoundLocation = @"E:\Smartzk\WindowsForms\WindowsForms\sound\button.wav";
p1.Load();
p1.Play();
能把这个做成类吗?如何做,C#winform。我想如果能够做成类,以后就可以直接调用了 展开
p1.SoundLocation = @"E:\Smartzk\WindowsForms\WindowsForms\sound\button.wav";
p1.Load();
p1.Play();
能把这个做成类吗?如何做,C#winform。我想如果能够做成类,以后就可以直接调用了 展开
7个回答
展开全部
可以的啊~比如这样:
public class MySound {
public static void play(String path)
{
System.Media.SoundPlayer sp= new System.Media.SoundPlayer();
sp.SoundLocation = path;
sp.Load();
sp.Play();
}
}
再每个按钮调用的时候你只需要
MySound.play("E:\Smartzk\WindowsForms\WindowsForms\sound\button.wav");
程序是直接打的~没有在IDE里调试~可能有错误~你自己看下思路就OK了~
public class MySound {
public static void play(String path)
{
System.Media.SoundPlayer sp= new System.Media.SoundPlayer();
sp.SoundLocation = path;
sp.Load();
sp.Play();
}
}
再每个按钮调用的时候你只需要
MySound.play("E:\Smartzk\WindowsForms\WindowsForms\sound\button.wav");
程序是直接打的~没有在IDE里调试~可能有错误~你自己看下思路就OK了~
更多追问追答
追问
这个地址有点问题,就是以后我做成安装包后,不一定是安装到相同的目录,那么它如何去以后安装的目录里寻找这个音乐文件呢?
追答
把你传进去的东西变一下不就OK了~
展开全部
写个方法:将要播放的文件名传入即可,在要添加音乐的按钮单击时间中直接调用,传入不同的声音文件名即可例如 play(“button.wav")!
仅供参考!
public void play(string name)
{
System.Media.SoundPlayer p1 = new System.Media.SoundPlayer();
p1.SoundLocation = @"E:\Smartzk\WindowsForms\WindowsForms\sound\"+name;
p1.Load();
p1.Play();
}
仅供参考!
public void play(string name)
{
System.Media.SoundPlayer p1 = new System.Media.SoundPlayer();
p1.SoundLocation = @"E:\Smartzk\WindowsForms\WindowsForms\sound\"+name;
p1.Load();
p1.Play();
}
追问
这个就是我的做法啊。但是我要用在平板电脑上,上面的操作都是使用button来操作的。所以按钮将非常多,所以我想把这个写成类来调用,以后只需要一句调用命令就搞定的那种。我对类还不够了解
追答
晕死。方法都有了,写成类还不容易?写成静态类不就可以了!引入命名空间,调用的时候用类名.方法名(参数)不就一句就OK了
我不理解楼主是什么意思?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
WINMM.Play(path.ToString());
[DllImport("Winmm.dll")]
public static extern bool PlaySound(String pszSound, IntPtr hmod, Int32 fdwSound);
/*
* flag values for fuSound and fdwSound arguments on [snd]PlaySound
*/
public const Int32 SND_SYNC = 0x0000; /* play synchronously (default) */
public const Int32 SND_ASYNC = 0x0001; /* play asynchronously */
public const Int32 SND_NODEFAULT = 0x0002; /* silence (!default) if sound not found */
public const Int32 SND_MEMORY = 0x0004; /* pszSound points to a memory file */
public const Int32 SND_LOOP = 0x0008; /* loop the sound until next sndPlaySound */
public const Int32 SND_NOSTOP = 0x0010; /* don't stop any currently playing sound */
public const Int32 SND_NOWAIT = 0x00002000; /* don't wait if the driver is busy */
public const Int32 SND_ALIAS = 0x00010000; /* name is a registry alias */
public const Int32 SND_ALIAS_ID = 0x00110000; /* alias is a predefined ID */
public const Int32 SND_FILENAME = 0x00020000; /* name is file name */
public const Int32 SND_RESOURCE = 0x00040004; /* name is resource name or atom */
public const Int32 SND_PURGE = 0x0040; /* purge non-static events for task */
public const Int32 SND_APPLICATION = 0x0080; /* look for application specific association */
[DllImport("winmm.dll")]
private static extern int mciSendString
(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
/*
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName
(
[MarshalAs(UnmanagedType.LPTStr)] string path,
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath,
int shortPathLength
);
*/
public static void Play(string FileName)
{
//StringBuilder shortPathTemp = new StringBuilder(255);
//int result = GetShortPathName(FileName, shortPathTemp, shortPathTemp.Capacity);
// string ShortPath = shortPathTemp.ToString();
mciSendString("open " + FileName + " alias song", "", 0, 0);
mciSendString("play song","",0,0);
}
public static void Stop()
{
mciSendString("close all", "", 0, 0);
}
public static void Pause()
{
mciSendString("pause song","",0,0);
}
public static void Close()
{
mciSendString("close song","",0,0);
}
}
[DllImport("Winmm.dll")]
public static extern bool PlaySound(String pszSound, IntPtr hmod, Int32 fdwSound);
/*
* flag values for fuSound and fdwSound arguments on [snd]PlaySound
*/
public const Int32 SND_SYNC = 0x0000; /* play synchronously (default) */
public const Int32 SND_ASYNC = 0x0001; /* play asynchronously */
public const Int32 SND_NODEFAULT = 0x0002; /* silence (!default) if sound not found */
public const Int32 SND_MEMORY = 0x0004; /* pszSound points to a memory file */
public const Int32 SND_LOOP = 0x0008; /* loop the sound until next sndPlaySound */
public const Int32 SND_NOSTOP = 0x0010; /* don't stop any currently playing sound */
public const Int32 SND_NOWAIT = 0x00002000; /* don't wait if the driver is busy */
public const Int32 SND_ALIAS = 0x00010000; /* name is a registry alias */
public const Int32 SND_ALIAS_ID = 0x00110000; /* alias is a predefined ID */
public const Int32 SND_FILENAME = 0x00020000; /* name is file name */
public const Int32 SND_RESOURCE = 0x00040004; /* name is resource name or atom */
public const Int32 SND_PURGE = 0x0040; /* purge non-static events for task */
public const Int32 SND_APPLICATION = 0x0080; /* look for application specific association */
[DllImport("winmm.dll")]
private static extern int mciSendString
(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
/*
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName
(
[MarshalAs(UnmanagedType.LPTStr)] string path,
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath,
int shortPathLength
);
*/
public static void Play(string FileName)
{
//StringBuilder shortPathTemp = new StringBuilder(255);
//int result = GetShortPathName(FileName, shortPathTemp, shortPathTemp.Capacity);
// string ShortPath = shortPathTemp.ToString();
mciSendString("open " + FileName + " alias song", "", 0, 0);
mciSendString("play song","",0,0);
}
public static void Stop()
{
mciSendString("close all", "", 0, 0);
}
public static void Pause()
{
mciSendString("pause song","",0,0);
}
public static void Close()
{
mciSendString("close song","",0,0);
}
}
追问
这个好复杂哦,我也希望能够搞懂,还在研究中,复杂得很。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以做成类的一个方法。
然后把路径做成一个参数传进去就行了。
然后把路径做成一个参数传进去就行了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Class MyMedia
{
private System.Media.SoundPlayer p1 ;
public MyMedia(string path)
{
p1 = new System.Media.SoundPlayer();
p1.SoundLocation = path;
}
public void Play()
{
p1.Load();
p1.Play();
}
}
用的时候new一个对象,MyMedia mm = new MyMedia("E:\Smartzk\WindowsForms\WindowsForms\sound\button.wav");
然后就可以对每个按钮调用这个mm对象了...
{
private System.Media.SoundPlayer p1 ;
public MyMedia(string path)
{
p1 = new System.Media.SoundPlayer();
p1.SoundLocation = path;
}
public void Play()
{
p1.Load();
p1.Play();
}
}
用的时候new一个对象,MyMedia mm = new MyMedia("E:\Smartzk\WindowsForms\WindowsForms\sound\button.wav");
然后就可以对每个按钮调用这个mm对象了...
追问
上面的代码我已经添加到类里面去了,但是如何调用呢?调用不出来
追答
调用不出来的原因可能是你这个类没有写为public吧。。这样看看咯
public Class MyMedia
{
private System.Media.SoundPlayer p1 ;
public MyMedia(string path)
{
p1 = new System.Media.SoundPlayer();
p1.SoundLocation = path;
}
public void Play()
{
p1.Load();
p1.Play();
}
}
如果你这个类写为嵌套类,又是另外一回事了...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
路过学习下
追问
别学啊,懂的就告诉我?有没有干瞪眼的设计程序啊?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询