《rvc#mp3播放器代码》由会员分享,可在线阅读,更多相关《rvc#mp3播放器代码(8页珍藏版)》请在金锄头文库上搜索。
1、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.Media; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Threading;na
2、mespace MyMusicPlays public partial class Form1 : FormThread t, d;Dictionary dicMusic = new Dictionary();MusicInfo musicInfo = null;int subId = 0;DateTime openTheMusic;public Form1()InitializeComponent();public static string SaveMusic = “D:SaveMusic.db“;private void button1_Click(object sender, Even
3、tArgs e)/播放、暂停按钮private void button1_Click_1(object sender, EventArgs e)if (myMusicPlay.playState= WMPLib.WMPPlayState.wmppsUndefined | myMusicPlay.playState= WMPLib.WMPPlayState.wmppsPaused | myMusicPlay.playState= WMPLib.WMPPlayState.wmppsStopped) myMusicPlay.Ctlcontrols.play();btnStart.Text = “暂停
4、(timer1.Start();elsemyMusicPlay.Ctlcontrols.pause();btnStart.Text = “播放(timer1.Stop();/停止按钮private void button3_Click(object sender, EventArgs e)myMusicPlay.Ctlcontrols.stop();btnStart.Text = “播放(pbMessage.Value = 0;timer1.Stop();/上传本地音乐private void linkLabel1_LinkClicked(object sender, LinkLabelLin
5、kClickedEventArgs e)OpenFileDialog op = new OpenFileDialog();op.Filter = “mp3 音乐音频(*.mp3)|*.mp3|wma 音乐音频 (*.wma)|*.wma“;DialogResult dr = op.ShowDialog();if (dr = DialogResult.OK)musicInfo = new MusicInfo();/获取最后一个的位置int lastIndex = op.FileName.LastIndexOf(“);/获取文件名字string musicName = op.FileName.Su
6、bstring(lastIndex + 1);musicInfo.MusicId = dicMusic.Count = 0 ? 1 : dicMusic.Count + 1;musicInfo.MusicName = musicName;musicInfo.MusicUrl = op.FileName;musicInfo.MusicTime = “00:00“;fillListView(musicInfo);dicMusic.Add(musicInfo.MusicId, musicInfo);/填充表格的方法private void fillListView(MusicInfo mi)List
7、ViewItem item = new ListViewItem(mi.MusicId.ToString();item.SubItems.AddRange(new string mi.MusicName,mi.MusicTime );lsvItems.Items.Add(item);private void lsvItems_SelectedIndexChanged(object sender, EventArgs e)/动态显示时间private void runTime()while (true)tryTimeSpan span = DateTime.Now - openTheMusic;
8、string musicTime = span.ToString().Substring(3,5);lblMusicPlayTime.Invoke(new MethodInvoker(delegate lblMusicPlayTime.Text = “当前进度:“ + musicTime; );catch(Exception)return;Thread.Sleep(1000);/ / 通过线程去播放音乐/ private void run()myMusicPlay.Ctlcontrols.stop();/控件事件委托(在线程里面失效)trylsvItems.Invoke(new MethodI
9、nvoker(delegate subId = int.Parse(lsvItems.SelectedItems0.SubItems0.Text); );catch (Exception )return;string url = dicMusicsubId.MusicUrl;myMusicPlay.URL = url;myMusicPlay.Ctlcontrols.play();/保存音乐-序列化文件private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)FileStream fs =
10、 new FileStream(SaveMusic, FileMode.Create);BinaryFormatter bf = new BinaryFormatter();bf.Serialize(fs, dicMusic);fs.Close();MessageBox.Show(“音乐文件保存成功!“,“提示 “,MessageBoxButtons.OK,MessageBoxIcon.Information);/窗体加载时显示出保存文件信息private void Form1_Load(object sender, EventArgs e)skinEngine1.SkinFile = “Pa
11、geColor2.ssk“;myMusicPlay.settings.volume = tbMessage.Value;lblMusicPlayTime.Text = “;lblShowMusicTime.Text = “;/读取loading(readMessage();/加载时候 fill 表格private void loading(Dictionary dicMusic)foreach (MusicInfo mi in dicMusic.Values)ListViewItem item = new ListViewItem(mi.MusicId.ToString();item.SubI
12、tems.AddRange(new string mi.MusicName, mi.MusicTime );lsvItems.Items.Add(item);/读取本地信息private Dictionary readMessage()tryFileStream fs = new FileStream(SaveMusic, FileMode.Open);BinaryFormatter bf = new BinaryFormatter();dicMusic = (Dictionary)bf.Deserialize(fs);fs.Close();catch (Exception) FileStre
13、am fs = new FileStream(SaveMusic, FileMode.Create); fs.Close(); return dicMusic;private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)showTheMusicTimes();/ / 获取歌曲长度/ private void showTheMusicTimes()/检查状态if (myMusicPlay.playState = WMPLib.WMPPlayState.wmppsStopped | myMus
14、icPlay.playState = WMPLib.WMPPlayState.wmppsUndefined| subId = 0)return;if (lsvItems.SelectedItems.Count / 滚动时候改变音量大小/ / / private void tbMessage_Scroll(object sender, EventArgs e)myMusicPlay.settings.volume = tbMessage.Value;/减少音量图片按钮private void pictureBox1_Click(object sender, EventArgs e)if (tbM
15、essage.Value = 5)tbMessage.Value += -5;myMusicPlay.settings.volume = tbMessage.Value;private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)Form2 f2 = new Form2();f2.Show();private void lsvItems_DoubleClick(object sender, EventArgs e)if (lsvItems.SelectedItems.Count = 0) return;btnStart.Enabled = true;t = new Thread(new ThreadStart(run);t.Start();pbMessage.Value = 0;/进度条工作.获取预播放曲目的时长string strTimes = lsvItems.SelectedItems0.SubItems2.Text;if (strTimes = “00:00“)showTheMusicTimes();strTimes = lsvItems.SelectedItems0.SubItems2.Text;DateTime times = Convert.ToDateTim