这么难吗?唉 我把代码贴出来吧,直观点…… using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; using System.Text; using System.Threading;
namespace PlayMusic { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private string filePath = null; private Audio audio = new Audio(); private Thread music = null; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button5; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.StatusBar statusBar1; private System.Windows.Forms.StatusBarPanel SBP_FN; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null;
public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent();
// // TOD 在 InitializeComponent 调用后添加任何构造函数代码 // }
/// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.button5 = new System.Windows.Forms.Button(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.statusBar1 = new System.Windows.Forms.StatusBar(); this.SBP_FN = new System.Windows.Forms.StatusBarPanel(); ((System.ComponentModel.ISupportInitialize)(this.SBP_FN)).BeginInit(); this.SuspendLayout(); // // button1 // this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.button1.Location = new System.Drawing.Point(30, 16); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(100, 32); this.button1.TabIndex = 0; this.button1.Text = "打开"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.button2.Location = new System.Drawing.Point(30, 56); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(100, 32); this.button2.TabIndex = 1; this.button2.Text = "播放"; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button3.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.button3.Location = new System.Drawing.Point(30, 96); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(100, 32); this.button3.TabIndex = 2; this.button3.Text = "暂停"; // // button4 // this.button4.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button4.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.button4.Location = new System.Drawing.Point(30, 136); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(100, 32); this.button4.TabIndex = 3; this.button4.Text = "停止"; // // button5 // this.button5.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button5.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.button5.Location = new System.Drawing.Point(30, 176); this.button5.Name = "button5"; this.button5.Size = new System.Drawing.Size(100, 32); this.button5.TabIndex = 4; this.button5.Text = "退出"; this.button5.Click += new System.EventHandler(this.button5_Click); // // openFileDialog1 // this.openFileDialog1.Filter = "wma文件 | *.wma"; this.openFileDialog1.Title = "打开文件..."; // // statusBar1 // this.statusBar1.Location = new System.Drawing.Point(0, 228); this.statusBar1.Name = "statusBar1"; this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] { this.SBP_FN}); this.statusBar1.ShowPanels = true; this.statusBar1.Size = new System.Drawing.Size(152, 22); this.statusBar1.TabIndex = 5; this.statusBar1.Text = "statusBar1"; // // SBP_FN // this.SBP_FN.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring; this.SBP_FN.Width = 136; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(152, 250); this.Controls.Add(this.statusBar1); this.Controls.Add(this.button5); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.MinimumSize = new System.Drawing.Size(160, 280); this.Name = "Form1"; this.Text = "Form1"; this.Resize += new System.EventHandler(this.Centralization); ((System.ComponentModel.ISupportInitialize)(this.SBP_FN)).EndInit(); this.ResumeLayout(false);
} #endregion
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); }
private void button5_Click(object sender, System.EventArgs e) { Application.Exit(); }
private void Centralization(object sender, System.EventArgs e) { this.Height = 280; button1.Left = button2.Left = button3.Left = button4.Left = button5.Left = this.Width/2 - this.button1.Width/2; }
private void play() { audio.Play(filePath); }
private void button1_Click(object sender, System.EventArgs e) { if(this.openFileDialog1.ShowDialog() == DialogResult.OK) { filePath = this.openFileDialog1.FileName; }
SBP_FN.Text = filePath.Substring(filePath.LastIndexOf("\\")+1, filePath.Length - filePath.LastIndexOf("\\") - 1); }
public void ThreadPlay() { music = new Thread(new ThreadStart(this.play)); music.Start();; }
private void button2_Click(object sender, System.EventArgs e) { ThreadPlay(); }
#region class Audio public class Audio { [DllImport("winmm.dll")] private static extern int mciSendString( string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback ); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern int GetShortPathName( string path, StringBuilder shortPath, int shortPathLength );
public void Play(string FileName) { StringBuilder shortPathTemp = new StringBuilder(255); int result = GetShortPathName(FileName, shortPathTemp, shortPathTemp.Capacity); string ShortPath = shortPathTemp.ToString();
mciSendString("open "+ShortPath+" alias song","",0,0); mciSendString("play song","",0,0); }
public void Stop() { mciSendString("stop song","",0,0); }
public void Pause() { mciSendString("pause song","",0,0); }
public void Close() { mciSendString("close song","",0,0); } } #endregion } }
[此贴子已经被作者于2005-8-15 18:53:19编辑过]
楼主,我把你没有实现的也实现了。下面是原代码:(仅供参考) using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; using System.Text; using System.Threading;
namespace PlayMusic { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private string filePath = null; private Audio audio = new Audio(); private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button5; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.StatusBar statusBar1; private System.Windows.Forms.StatusBarPanel SBP_FN; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null;
public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent();
// // TOD 在 InitializeComponent 调用后添加任何构造函数代码 // }
/// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.button5 = new System.Windows.Forms.Button(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.statusBar1 = new System.Windows.Forms.StatusBar(); this.SBP_FN = new System.Windows.Forms.StatusBarPanel(); ((System.ComponentModel.ISupportInitialize)(this.SBP_FN)).BeginInit(); this.SuspendLayout(); // // button1 // this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.button1.Location = new System.Drawing.Point(30, 16); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(100, 32); this.button1.TabIndex = 0; this.button1.Text = "打开"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.button2.Location = new System.Drawing.Point(30, 56); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(100, 32); this.button2.TabIndex = 1; this.button2.Text = "播放"; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button3.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.button3.Location = new System.Drawing.Point(30, 96); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(100, 32); this.button3.TabIndex = 2; this.button3.Text = "暂停"; this.button3.Click += new System.EventHandler(this.button3_Click); // // button4 // this.button4.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button4.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.button4.Location = new System.Drawing.Point(30, 136); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(100, 32); this.button4.TabIndex = 3; this.button4.Text = "停止"; this.button4.Click += new System.EventHandler(this.button4_Click); // // button5 // this.button5.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button5.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.button5.Location = new System.Drawing.Point(30, 176); this.button5.Name = "button5"; this.button5.Size = new System.Drawing.Size(100, 32); this.button5.TabIndex = 4; this.button5.Text = "退出"; this.button5.Click += new System.EventHandler(this.button5_Click); // // openFileDialog1 // this.openFileDialog1.Filter = "wma文件 | *.wma"; this.openFileDialog1.Title = "打开文件..."; // // statusBar1 // this.statusBar1.Location = new System.Drawing.Point(0, 231); this.statusBar1.Name = "statusBar1"; this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] { this.SBP_FN}); this.statusBar1.ShowPanels = true; this.statusBar1.Size = new System.Drawing.Size(152, 22); this.statusBar1.TabIndex = 5; this.statusBar1.Text = "statusBar1"; // // SBP_FN // this.SBP_FN.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring; this.SBP_FN.Width = 136; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.BackColor = System.Drawing.Color.OliveDrab; this.ClientSize = new System.Drawing.Size(152, 253); this.ControlBox = false; this.Controls.Add(this.statusBar1); this.Controls.Add(this.button5); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MinimumSize = new System.Drawing.Size(160, 280); this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "播放器"; this.Resize += new System.EventHandler(this.Centralization); ((System.ComponentModel.ISupportInitialize)(this.SBP_FN)).EndInit(); this.ResumeLayout(false);
} #endregion
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); }
private void button5_Click(object sender, System.EventArgs e) { Application.Exit(); }
private void Centralization(object sender, System.EventArgs e) { this.Height = 280; button1.Left = button2.Left = button3.Left = button4.Left = button5.Left = this.Width/2 - this.button1.Width/2; }
private void play() { audio.Play(filePath); } public void pause() { this.audio.Pause(); } public void stop() { this.audio.Stop(); } public void close() { this.audio.Close(); }
private void button1_Click(object sender, System.EventArgs e) { if(this.openFileDialog1.ShowDialog() == DialogResult.OK) { filePath = this.openFileDialog1.FileName; }
SBP_FN.Text = filePath.Substring(filePath.LastIndexOf("\\")+1, filePath.Length - filePath.LastIndexOf("\\") - 1); }
private void button2_Click(object sender, System.EventArgs e) { if(this.SBP_FN.Text!=null) { this.audio.Close(); filePath = this.openFileDialog1.FileName; this.play(); } else { this.play(); } }
private void button3_Click(object sender, System.EventArgs e) { this.pause(); }
private void button4_Click(object sender, System.EventArgs e) { this.stop(); }
#region class Audio public class Audio { [DllImport("winmm.dll")] private static extern int mciSendString ( string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback );
[DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern int GetShortPathName ( [MarshalAs(UnmanagedType.LPTStr)] string path, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath, int shortPathLength );
public void Play(string FileName) { StringBuilder shortPathTemp = new StringBuilder(255); int result = GetShortPathName(FileName, shortPathTemp, shortPathTemp.Capacity); string ShortPath = shortPathTemp.ToString();
mciSendString("open "+ShortPath+" alias song"," ",0,0); mciSendString("play song"," ",0,0); }
public void Stop() { mciSendString("stop song","",0,0); }
public void Pause() { mciSendString("pause song","",0,0); }
public void Close() { mciSendString("close song","",0,0); } } #endregion } }
[此贴子已经被作者于2005-8-16 14:17:42编辑过]