C#学习11
使用Windows窗体应用程序,基于Windows Media Player组件写一个简单的播放器,功能比较简陋,通过打开文件按钮来选取音乐所在的地址,还有4个按钮,分别是:播放,结束,暂停/继续,歌曲信息
添加AxWindowsMediaPlayer控件的方法:在”工具箱”中单击右键,选择”选择项”菜单,打开”选择工具箱项”窗口,选择”COM”组件”标签,在列表中找到并勾选”Windows Media Player”组件,单击”确定”按钮。将该组件添加到指定的工具箱选项卡中,然后在工具箱里面找 Windows Media Player 控件,拉到form里面即可
OpenFileDialog控件也许注意
赋个截图,按暂停按钮时停止播放,再按一次就继续播放
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; namespace cam { public partial class Cambridge : Form { public Cambridge() { InitializeComponent(); } private void ButPlay_Click(object sender, EventArgs e) //加载多媒体文件 { this.axWindowsMediaPlayer1.URL = this.optFile.FileName; } private void ButOpen_Click(object sender, EventArgs e) { this.optFile.ShowDialog(); this.axWindowsMediaPlayer1.newMedia(this.optFile.FileName); } private void ButStop_Click(object sender, EventArgs e) //停止 { this.axWindowsMediaPlayer1.Ctlcontrols.stop(); } private void ButPause_Click(object sender, EventArgs e) { if (this.ButPause.Text == "暂停") //暂停播放 { this.axWindowsMediaPlayer1.Ctlcontrols.pause(); this.ButPause.Text = "继续"; } else { this.axWindowsMediaPlayer1.Ctlcontrols.play();//继续播放 this.ButPause.Text = "暂停"; } } private WMPLib.WindowsMediaPlayerClass c;//媒体对象 private WMPLib.IWMPMedia m; //建立媒体播放列表对象 private void ButInfo_Click(object sender, EventArgs e) { if (this.optFile.FileName != "optFile") //实例化WindowsMediaPlayerClass类的对象 { c = new WMPLib.WindowsMediaPlayerClass(); m = c.newMedia(this.optFile.FileName); MessageBox.Show("歌手名:" + m.getItemInfo("Author") + "\r\n" + "歌名:" + m.getItemInfo("Title")); } } } }