//WAV声音播放
//-------------------------------------
[DllImport("winmm.dll")]
public static extern int mciSendString(string m_strCmd, string m_strReceive, int m_v1, int m_v2);
[DllImport("winmm.dll")]
public static extern int mciSendString(string m_strCmd, StringBuilder m_strReceive, int m_v1, int m_v2);
[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);
//-------------------------------------
//播放WAV声音
public static void playMusic(string name, string Vol)
{
try
{
StringBuilder shortpath = new StringBuilder(100);
int result = GetShortPathName(name, shortpath, shortpath.Capacity);
name = shortpath.ToString();
string buf = string.Empty;
mciSendString("play " + name, buf, buf.Length, 0); //播放
mciSendString("SETAUDIO " + name + " VOLUME TO " + Vol, buf, 0, 0); //音量1-1000
return;
}
catch (Exception ex)
{
MessageBox.Show("playMusic:" + ex.Message);
return;
}
}
//检测声音的长度
public static void LoadMusicTime(string name)
{
try
{
StringBuilder shortpath = new StringBuilder(100);
int result = GetShortPathName(name, shortpath, shortpath.Capacity);
name = shortpath.ToString();
StringBuilder buf = new StringBuilder(80);
mciSendString("close all", buf, buf.Capacity, 0);
mciSendString("open " + name + " alias media", buf, buf.Capacity, 0);
mciSendString("status media length", buf, buf.Capacity, 0);
TimeSpan ts = new TimeSpan(0, 0, 0, 0, (int)Convert.ToDouble(buf.ToString().Trim()));
MessageBox.Show( ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds + "." + ts.Milliseconds);
mciSendString("close all", buf, buf.Capacity, 0);
return;
}
catch (Exception ex)
{
MessageBox.Show("LoadMusicTime:" + ex.Message);
return;
}
}
//停止播放
public static void StopMusic()
{
try
{
StringBuilder buf = new StringBuilder(80);
mciSendString("close all", buf, buf.Capacity, 0);
return;
}
catch (Exception ex)
{
MessageBox.Show("StopMusic:" + ex.Message);
return;
}
}