using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace AudioPlayer { class MP3Player { [DllImport("winmm.dll")] private static extern int mciSendString(String command, StringBuilder buffer, int bufferSize, IntPtr hwndCallback); private string m_szFilePath = ""; public string FilePath { get { return FilePath; } } private int m_iError = 0; public int LastErrorCode { get { return m_iError; } } private ulong m_ulLength = 0; public ulong AudioLength { get { if (m_bOpened == true) { return m_ulLength; } else { return 0; } } } private bool m_bOpened = false; private bool m_bPlaying = false; private bool m_bPaused = false; private bool m_bLoop = false; private void SendString(string szCommand) { int iError = mciSendString(szCommand, null, 0, IntPtr.Zero); if (iError != 0) m_iError = iError; } private void CalculateLength() { StringBuilder sb = new StringBuilder(128); mciSendString("status MediaFile length", sb, 128, IntPtr.Zero); m_ulLength = Convert.ToUInt64(sb.ToString()); } public void Open(string szFilePath) { if (m_bOpened == false) { SendString("open \"" + szFilePath + "\" type mpegvideo alias MediaFile"); m_szFilePath = szFilePath; m_bOpened = true; m_bPlaying = false; m_bPaused = false; SendString("set MediaFile time format milliseconds"); SendString("set MediaFile seek exactly on"); CalculateLength(); } else { Close(); Open(szFilePath); } } public void Close() { if (m_bOpened == true) { SendString("close MediaFile"); m_bOpened = false; m_bPlaying = false; m_bPaused = false; } } public void Seek(ulong ulMillisecs) { if (m_bOpened == true && m_bPlaying == true && ulMillisecs <= m_ulLength) { if (m_bPaused == true) { SendString(String.Format("seek MediaFile to {0}", ulMillisecs)); } else { SendString(String.Format("seek MediaFile to {0}", ulMillisecs)); if (m_bLoop == true) { SendString("play MediaFile REPEAT"); } else { SendString("play MediaFile"); } } } } public void Play() { if (m_bOpened == true) { if (m_bPlaying == false) { m_bPlaying = true; SendString("play MediaFile"); } else { if (m_bPaused == false) { SendString("seek MediaFile to start"); SendString("play MediaFile"); } else { m_bPaused = false; SendString("play MediaFile"); } } } } public void LoopPlay() { if (m_bOpened == true) { m_bLoop = true; if (m_bPlaying == false) { m_bPlaying = true; SendString("play MediaFile REPEAT"); } else { if (m_bPaused == false) { SendString("seek MediaFile to start"); SendString("play MediaFile REPEAT"); } else { m_bPaused = false; SendString("play MediaFile REPEAT"); } } } } public void Pause() { if (m_bOpened == true) { if (m_bPaused == false) { m_bPaused = true; SendString("pause MediaFile"); } else { m_bPaused = false; if (m_bLoop == true) { SendString("play MediaFile REPEAT"); } else { SendString("play MediaFile"); } } } } public void Stop() { if (m_bOpened == true && m_bPlaying == true) { m_bPlaying = false; m_bPaused = false; m_bLoop = false; SendString("seek MediaFile to start"); SendString("stop MediaFile"); } } } }