110,534
社区成员
发帖
与我相关
我的任务
分享
using System.Runtime.InteropServices;
class Music
{
[DllImport("winmm.dll")]
public static extern int mciSendString(string m_strCmd, string m_strReceive, int m_v1, int m_v2);
[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);
private void playMusic(string name)
{
try
{
StringBuilder shortpath = new StringBuilder(80);
int result = GetShortPathName(name, shortpath, shortpath.Capacity);
name = shortpath.ToString();
string buf = string.Empty;
mciSendString("play " + name, buf, buf.Length, 0); //播放
}
catch (Exception ex)
{
throw ex;
}
}
private void playMusicRepeatly(string name)
{
try
{
StringBuilder shortpath = new StringBuilder(80);
int result = GetShortPathName(name, shortpath, shortpath.Capacity);
name = shortpath.ToString();
string buf = string.Empty;
mciSendString("play " + name+" repeat", buf, buf.Length, 0); //播放
}
catch (Exception ex)
{
throw ex;
}
}
public void stopMusic(string name)
{
StringBuilder shortpath = new StringBuilder(80);
int result = GetShortPathName(name, shortpath, shortpath.Capacity);
name = shortpath.ToString();
string buf = string.Empty;
mciSendString(@"stop "+name, buf, buf.Length, 0);
mciSendString(@"close "+name, buf, buf.Length, 0); //停止播放
}
}