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.IO;using WMPLib;using System.Runtime.Serialization.Formatters.Binary;using System.Threading;namespace 播放器{ public partial class Form1 : Form { public Form1() { InitializeComponent(); MediaPlayer.settings.setMode("loop", true); } static int num = 1; static bool kuaijin = false; ListViewItem item; private void 打开文件夹ToolStripMenuItem_Click(object sender, EventArgs e) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { string path = folderBrowserDialog1.SelectedPath;//打开文件夹的路径 string[] str = Directory.GetFiles(path); for (int i = 0; i < str.Length; i ) { item = new ListViewItem(); item.Text = num.ToString(); if (Path.GetExtension(str[i]) == ".mp3") { item.SubItems.Add(Path.GetFileName(str[i])); item.SubItems.Add(str[i]); listView1.Items.Add(item); IWMPMedia song = MediaPlayer.newMedia(@str[i]); MediaPlayer.currentPlaylist.appendItem(song); num ; } } } MediaPlayer.Ctlcontrols.play(); } private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { string[] sdr = openFileDialog1.SafeFileNames; string[] str = openFileDialog1.FileNames; for (int i =0; i<str.Length; i ) { ListViewItem item = new ListViewItem(); item.Text = num.ToString(); item.SubItems.Add(sdr[i]); item.SubItems.Add(str[i]); listView1.Items.Add(item); IWMPMedia song = MediaPlayer.newMedia(@str[i]); MediaPlayer.currentPlaylist.appendItem(song); MediaPlayer.Ctlcontrols.playItem(song); num ; } } } private void listView1_DoubleClick(object sender, EventArgs e) { //想要优先播放某一首歌 int a = listView1.SelectedItems[0].Index; IWMPMedia song = MediaPlayer.currentPlaylist.get_Item(a); MediaPlayer.Ctlcontrols.playItem(song); } private void 播放ToolStripMenuItem_Click(object sender, EventArgs e) { MediaPlayer.Ctlcontrols.play(); } private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e) { MediaPlayer.Ctlcontrols.pause(); toolStripButton1.Text = "暂停"; } private void 停止ToolStripMenuItem_Click(object sender, EventArgs e) { MediaPlayer.Ctlcontrols.stop(); } private void 上一首ToolStripMenuItem_Click(object sender, EventArgs e) { MediaPlayer.Ctlcontrols.previous(); } private void 下一首ToolStripMenuItem_Click(object sender, EventArgs e) { MediaPlayer.Ctlcontrols.next(); } private void 打开列表ToolStripMenuItem_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { StreamReader reader = new StreamReader(openFileDialog1.FileName); string s=""; while ((s=reader.ReadLine())!=null) { ListViewItem idd = new ListViewItem(); idd.Text = num.ToString(); idd.SubItems.Add(Path.GetFileName(s)); idd.SubItems.Add(s); listView1.Items.Add(idd); IWMPMedia song = MediaPlayer.newMedia(s); MediaPlayer.currentPlaylist.appendItem(song); MediaPlayer.Ctlcontrols.play(); num ; } reader.Close(); } } private void 保存列表ToolStripMenuItem_Click(object sender, EventArgs e) { saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { StreamWriter writer = new StreamWriter(saveFileDialog1.FileName); string s = ""; for (int i = 0; i < listView1.Items.Count; i ) { s = listView1.Items[i].SubItems[2].Text; writer.WriteLine(s); } writer.Close(); } } private void 清空列表ToolStripMenuItem_Click(object sender, EventArgs e) { listView1.Items.Clear(); MediaPlayer.currentPlaylist.clear(); } private void 播放ToolStripMenuItem1_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count != 0) { int a = listView1.SelectedItems[0].Index; IWMPMedia song = MediaPlayer.currentPlaylist.get_Item(a); MediaPlayer.Ctlcontrols.playItem(song); } else { MessageBox.Show("请选中一首歌曲!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { try { if (listView1.SelectedItems.Count != 0) { int b = listView1.SelectedItems[0].Index; listView1.SelectedItems[0].Remove(); IWMPMedia song = MediaPlayer.currentPlaylist.get_Item(b); MediaPlayer.currentPlaylist.removeItem(song); if (b != listView1.Items.Count) { MediaPlayer.Ctlcontrols.playItem(MediaPlayer.currentPlaylist.get_Item(b)); } else { int c=Convert.ToInt32(listView1.Items[0].Text); MediaPlayer.Ctlcontrols.playItem(MediaPlayer.currentPlaylist.get_Item(c-1)); } } else { MessageBox.Show("请选中一首歌曲", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception a) { MessageBox.Show(a.Message); } } private void toolStripButton1_Click(object sender, EventArgs e) { if (toolStripButton1.Text == "播放") { MediaPlayer.Ctlcontrols.play(); toolStripButton1.Text = "暂停"; } else if (toolStripButton1.Text == "暂停") { MediaPlayer.Ctlcontrols.pause(); toolStripButton1.Text = "播放"; } } private void 单曲循环ToolStripMenuItem_Click(object sender, EventArgs e) { MediaPlayer.Ctlcontrols.playItem(MediaPlayer.currentPlaylist.Item[0]); } private void 快进ToolStripMenuItem_Click(object sender, EventArgs e) { if (kuaijin == false) { MediaPlayer.Ctlcontrols.fastForward(); 快进ToolStripMenuItem.Text = "播放"; kuaijin = true; } else { MediaPlayer.settings.rate = 1; 快进ToolStripMenuItem.Text = "快进"; kuaijin = false; } } private void 随机播放ToolStripMenuItem_Click(object sender, EventArgs e) { MediaPlayer.settings.setMode("shuffle", true); } private void 列表循环ToolStripMenuItem_Click(object sender, EventArgs e) { MediaPlayer.settings.setMode("loop", true); } private void 关于作者ToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("本软件由陈明保编写制作完成", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void toolStripButton3_Click(object sender, EventArgs e) { if (MediaPlayer.settings.mute == false) { MediaPlayer.settings.mute = true; tool3.Text = "播放"; } else { MediaPlayer.settings.mute = false; tool3.Text = "静音"; } } private void toolStripButton2_Click(object sender, EventArgs e) { MediaPlayer.Ctlcontrols.previous(); } private void toolStripButton3_Click_1(object sender, EventArgs e) { MediaPlayer.Ctlcontrols.next(); } private void toolStripButton4_Click(object sender, EventArgs e) { MediaPlayer.Ctlcontrols.stop(); } private void toolStripButton5_Click(object sender, EventArgs e) { MediaPlayer.Ctlcontrols.play(); } public void Showlrc() { StreamReader rd; string name = ""; rtbox.ReadOnly = true; string[] lrc = Directory.GetFiles(@"e:\lrc"); foreach (string geci in lrc) { rd = new StreamReader(geci); name = Path.GetFileNameWithoutExtension(geci); if (MediaPlayer.currentMedia.name == name) { string s = ""; while ((s=rd.ReadLine())!=null) { rtbox.AppendText("\t" s "\r"); } rd.Close(); } else { rtbox.AppendText(""); } } if (rtbox.TextLength == 0) { rtbox.AppendText("抱歉,没有搜索到歌词文件"); } } private void 属性ToolStripMenuItem_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count != 0) { string s = listView1.SelectedItems[0].SubItems[2].Text; DateTime time = File.GetCreationTime(s); string size = s.Length.ToString(); MessageBox.Show(string.Format("文件路径是{0}\n文件大小为{1}\n文件创建时间是{2}", s, size, time.ToShortDateString())); } else { MessageBox.Show("请选中一首歌曲!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void 顺序播放ToolStripMenuItem_Click(object sender, EventArgs e) { MediaPlayer.settings.setMode("showFrame", true); } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void 当前播放媒体信息ToolStripMenuItem_Click(object sender, EventArgs e) { string a = MediaPlayer.currentMedia.getItemInfo("Title"); string b = MediaPlayer.currentMedia.getItemInfo("Author"); string c = MediaPlayer.currentMedia.getItemInfo("FileType"); string d = MediaPlayer.currentMedia.getItemInfo("FileSize"); MessageBox.Show(string.Format("媒体标题是 {0}\n艺术家是 {1}\n文件类型为 {2}\n文件大小为{3}", a, b, c,d)); } private void 添加文件ToolStripMenuItem1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { string[] sdr = openFileDialog1.SafeFileNames; string[] str = openFileDialog1.FileNames; for (int i = 0; i < str.Length; i ) { ListViewItem item = new ListViewItem(); item.Text = num.ToString(); item.SubItems.Add(sdr[i]); item.SubItems.Add(str[i]); listView1.Items.Add(item); IWMPMedia song = MediaPlayer.newMedia(@str[i]); MediaPlayer.currentPlaylist.appendItem(song); MediaPlayer.Ctlcontrols.play(); num ; } } } //delegate void SetTimeCallback(int text); //delegate void SetValueCallback(int time, int i); //Thread t; private void MediaPlayer_CurrentItemChange(object sender, AxWMPLib._WMPOCXEvents_CurrentItemChangeEvent e) { int selectedindex = 0;//记录listview1当前高亮显示的项的下标 for (int i = 0; i < listView1.Items.Count; i ) { if (listView1.Items[i].SubItems[2].Text == MediaPlayer.currentMedia.sourceURL) { selectedindex = i; break; } } for (int i = 0; i < listView1.Items.Count; i ) { //listView1.Items[i].Selected = false; listView1.Items[i].ForeColor = Color.Black; } listView1.Focus(); //listView1.Items[selectedindex].Selected = true; listView1.Items[selectedindex].ForeColor = Color.RoyalBlue; rtbox.Clear(); Showlrc(); //progressBar1.Value = 0;//进度条清零 //string str=openFileDialog1.FileName; //FileInfo f = new FileInfo(str); ////进度条最大值是文件大小/1024 // progressBar1 .Maximum= (int)(f.Length/1024); ////创建流对象,读文件流 // FileStream fileread = new FileStream(str, FileMode.Open, FileAccess.Read); // //创建读取器,方便我们读取String // //StreamReader reader = new StreamReader(filestream); // byte[] bytes = new byte[1024*1024*4];//相对比较合理 // //循环读取自动偏移 // int j;//记录读取的实际字节数 // while ((j=fileread.Read(bytes, 0, bytes.Length)) != 0) // { // progressBar1.Value = j/1024; // } // string s = MediaPlayer.currentMedia.durationString; // string[] str = s.Split(':'); // int fen = s[0] * 60; // int miso = s[1]; // time = (fen miso)*1000; // t = new Thread(new ThreadStart(NewMethod)); // t.Start(); //} //private int time; //private void NewMethod() //{ // settime(time);//设置最大值 // for (int i = 1; i <= 100; i ) // { // SetValue(time, i);//设置进度条当前位置 // Thread.Sleep(time / 100);//等待多久跳一次 // } } //private void SetValue(int time, int i) //{ // //progressBar1.Value = i * (time / 100); // if (this.progressBar1.InvokeRequired) // { // SetValueCallback d = new SetValueCallback(SetValue); // this.Invoke(d, new object[] { time, i }); // } // else // { // this.progressBar1.Value = i * (time / 100); // } //} //private void settime(int time) //{ // if (this.progressBar1.InvokeRequired) // { // SetTimeCallback d = new SetTimeCallback(settime); // this.Invoke(d, new object[] { time }); // } // else // { // this.progressBar1.Maximum = time; // } //this.progressBar1.Maximum = time; //} private void Form1_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy; string[] str = (string[])e.Data.GetData(DataFormats.FileDrop, true); //string[] sdr = strDro //string[] str = openFileDialog1.FileNames; for (int i = 0; i < str.Length; i ) { ListViewItem item = new ListViewItem(); item.Text = num.ToString(); item.SubItems.Add(Path.GetFileName(str[i])); item.SubItems.Add(str[i]); listView1.Items.Add(item); IWMPMedia song = MediaPlayer.newMedia(str[i]); MediaPlayer.currentPlaylist.appendItem(song); MediaPlayer.Ctlcontrols.playItem(song); num ; } } } }
下载C#制作简易音乐播放器用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论