1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74:
| using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices;
namespace MP3 { public class MP3Player { private string _Alias;
public MP3Player(string pAlias) { this._Alias = pAlias; }
public bool Play(string pFileName) { bool ret = false; StringBuilder retApi = new StringBuilder(new string(' ', 256)); if (GetShortPathName(pFileName, retApi, retApi.Length) > 0) { int intMciResult = mciSendString(string.Format("open {0} type MPEGVideo alias {1}", retApi.ToString(), this._Alias), string.Empty, 0, IntPtr.Zero);
int intPlayResult = -1; if (intMciResult == 0) { intPlayResult = mciSendString(string.Format("play {0} from 0", this._Alias), string.Empty, 0, IntPtr.Zero); }
ret = intPlayResult == 0; } return ret; }
public bool Stop() { bool ret = false; if (mciSendString(string.Format("stop {0}", this._Alias), string.Empty, 0, IntPtr.Zero) == 0) { ret = mciSendString(string.Format("close {0}", this._Alias), string.Empty, 0, IntPtr.Zero) == 0; } return ret; } [DllImport("winmm.dll")] private static extern int mciSendString(string lpCommand, string lpReturn, int nReturnLength, IntPtr callBack);
[DllImport("kernel32")] private static extern int GetShortPathName(string lpLongPath, StringBuilder lpShortPath, int Buffer); } }
namespace DeviceApplication13 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } MP3.MP3Player _M = new MP3.MP3Player("myPlayer"); private void button1_Click(object sender, System.EventArgs e) { this._M.Play(@"\Storage Card\test.mp3"); } } } |