using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace GeneralLib { public partial class FrmPLSound : Form { public FrmPLSound() { InitializeComponent(); } private const int MM_MCINOTIFY = 0x3B9; [DllImport("winmm.dll")] private static extern Int32 mciSendString(String command, StringBuilder buffer, Int32 bufferSize, IntPtr hwndCallback); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern int GetShortPathName(string lpszLongPath, string shortFile, int cchBuffer); private void FrmPLSound_Load(object sender, EventArgs e) { } private bool IsLoop = false; private string PlayKey = ""; private string PlayPath = ""; protected override void DefWndProc(ref Message m) { base.DefWndProc(ref m); if (m.Msg == MM_MCINOTIFY) { if (m.WParam == (IntPtr)1) { if (IsLoop) { Play(PlayKey, PlayPath, true); } else { StopKey(PlayKey); //this.Close(); } } } } private string GetShortPath(string longPath) { string shortPath = ""; try { shortPath = "".PadLeft(520, Convert.ToChar(" ")); GetShortPathName(longPath, shortPath, shortPath.Length); int endChr = shortPath.LastIndexOf("\0"); if (endChr >= 0) shortPath = shortPath.Substring(0, endChr); } catch (Exception ex) { SystemLog.WriterLog(ex); } return shortPath; } public void Play(string key, string path, bool isLoop) { try { path = GetShortPath(path); StopKey(key); IsLoop = isLoop; PlayKey = key; PlayPath = path; mciSendString("open \"" + path + "\" alias " + key, null, 0, IntPtr.Zero); mciSendString("play " + key + " notify", null, 0, this.Handle); } catch (Exception ex) { SystemLog.WriterLog(ex); } } private void StopKey(string key) { mciSendString("stop " + key, null, 0, IntPtr.Zero); mciSendString("close " + key, null, 0, IntPtr.Zero); } public void Stop(string key) { IsLoop = false; StopKey(key); //this.Close(); } private void FrmPLSound_FormClosing(object sender, FormClosingEventArgs e) { IsLoop = false; StopKey(PlayKey); } } }