using
System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace MusicPlayer { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string [] musicPath = new string [ 10000 ]; // 用于保存歌曲目录 int musicCount = 0 ; private void Form1_Load( object sender, EventArgs e) { myPlyer.BeginInit(); // 初始化 myPlyer.settings.autoStart = true ; // 自动播放 myPlyer.settings.setMode( " shuffle " , false ); // 顺序播放 myPlyer.settings.enableErrorDialogs = true ; myPlyer.settings.balance = 0 ; myPlyer.settings.mute = false ; myPlyer.settings.volume = 100 ; // 声音设为最大 btnBack.Enabled = false ; // 声音不对 btnForward.Enabled = false ; // 声音不对 btnBE.Enabled = false ; // 无法暂停和开始 ExitToolStripMenuItem.Enabled = false ; // 无法退出 ReplayToolStripMenuItem.Enabled = false ; // 无法单曲循环 if (File.Exists( " listbox.txt " )) // 如果存在播放列表,那么加载播放列表 { StreamReader reader = new StreamReader( " listbox.txt " ); try { while (reader.Peek() != - 1 ) { string filepath = reader.ReadLine(); if (File.Exists(filepath)) { musicPath[musicCount++] = filepath; string filename = Path.GetFileName(filepath); listBox1.Items.Add(filename); // listbox用来显示歌曲名 myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count, myPlyer.newMedia(filepath)); } } listBox1.SelectedIndex = 0 ; } catch (Exception) { listBox1.SelectedIndex = - 1 ; MessageBox.Show( " 加载播放列表失败! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { reader.Close(); } } } private void OpenToolStripMenuItem_Click( object sender, EventArgs e) // 打开音乐文件,但不加入到播放列表中 { DialogResult dr = openFileDialog1.ShowDialog(); if (dr == DialogResult.OK) { myPlyer.URL = openFileDialog1.FileName; } } private void ExitToolStripMenuItem_Click( object sender, EventArgs e) // 结束程序,但为什么不起作用?而共享此动作的btnExit却有作用? { myPlyer.Ctlcontrols.stop(); myPlyer.close(); Application.Exit(); } private void AddSingleToolStripMenuItem_Click( object sender, EventArgs e) // 添加单首歌曲到播放列表中,"添加"按钮共享此事件 { DialogResult dr = openFileDialog1.ShowDialog(); if (dr == DialogResult.OK) { string filepath = openFileDialog1.FileName; string filename = Path.GetFileName(filepath); listBox1.Items.Add(filename); musicPath[musicCount++] = filepath; myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count,myPlyer.newMedia(filepath)); } } private void AddMoreToolStripMenuItem_Click( object sender, EventArgs e) // 添加选中的文件夹中的mp3文件到播放列表中 { DialogResult dr = folderBrowserDialog1.ShowDialog(); if (dr == DialogResult.OK) { string [] filepath = Directory.GetFiles(folderBrowserDialog1.SelectedPath); foreach ( string s in filepath) { if (Path.GetExtension(s) == " .mp3 " ) { string filename = Path.GetFileName(s); listBox1.Items.Add(filename); musicPath[musicCount++] = s; myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count, myPlyer.newMedia(s)); } } } } private void listBox1_DoubleClick( object sender, EventArgs e) // 播放列表中选中的歌曲,随机播放状态下不起作用 { int j = listBox1.SelectedIndex; if (listBox1.Items.Count> 0 ) { myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(j)); } } private void Form1_FormClosing( object sender, FormClosingEventArgs e) // 退出程序的动作 { myPlyer.Ctlcontrols.stop(); myPlyer.close(); // 关闭播放器 StreamWriter writer = new StreamWriter( " listbox.txt " , false , Encoding.Unicode); // 保存播放列表 for ( int i = 0 ; i <=musicCount- 1 ; i++) { if (musicPath[i] != string .Empty) { writer.WriteLine(musicPath[i]); } } writer.Close(); } private void axWindowsMediaPlayer1_PlayStateChange( object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e) // 显示播放状态 { if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying) { label1.Text = " 正在播放 " + myPlyer.currentMedia.sourceURL+ " " +myPlyer.currentMedia.durationString; string s = myPlyer.currentMedia.sourceURL; for ( int i = 0 ; i < myPlyer.currentPlaylist.count; i++) { if (listBox1.Items[i].ToString() == Path.GetFileName(s)) { listBox1.SelectedIndex = i; break ; } } } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsBuffering) { label1.Text = " 正在缓冲 " + myPlyer.currentMedia.sourceURL; } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPaused) { label1.Text = " 暂停播放 " + myPlyer.currentMedia.sourceURL; } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsWaiting) { label1.Text = " 正在等待 " ; } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsStopped) { label1.Text = " 播放停止 " ; } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsReady) { label1.Text = " 准备就绪 " ; } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsScanForward) { label1.Text = " 正在快进 " + myPlyer.currentMedia.sourceURL; } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsScanReverse) { label1.Text = " 正在快退 " + myPlyer.currentMedia.sourceURL; } } private void btnBE_Click( object sender, EventArgs e) // 暂停/开始,不起作用 { if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying) { myPlyer.Ctlcontrols.pause(); } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPaused) { myPlyer.Ctlcontrols.play(); } } private void btnStop_Click( object sender, EventArgs e) // 停止播放 { myPlyer.Ctlcontrols.stop(); } private void btnSlient_Click( object sender, EventArgs e) // 静音 { if (myPlyer.settings.mute == false ) { myPlyer.settings.mute = true ; } else { myPlyer.settings.mute = false ; } } private void btnBack_Click( object sender, EventArgs e) // 快退,声音不对 { if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying) { myPlyer.Ctlcontrols.fastReverse(); } } private void btnForward_Click( object sender, EventArgs e) // 快进,声音不对 { if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying) { myPlyer.Ctlcontrols.fastForward(); } } private void btnPre_Click( object sender, EventArgs e) // 上一曲 { if (listBox1.SelectedIndex != 0 ) { myPlyer.Ctlcontrols.previous(); } } private void btnNext_Click( object sender, EventArgs e) // 下一曲 { if (listBox1.SelectedIndex != listBox1.Items.Count - 1 ) { myPlyer.Ctlcontrols.next(); } } private void btnPlay_Click( object sender, EventArgs e) // 双击播放列表中选中的歌曲 { if (listBox1.Items.Count> 0 &&listBox1.SelectedIndex>= 0 ) { myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(listBox1.SelectedIndex)); } } private void btRemove_Click( object sender, EventArgs e) // 将选中的歌曲移出播放列表 { int i = listBox1.SelectedIndex; for ( int j = 0 ; j < musicCount; j++) { if (Path.GetFileName(musicPath[j]) == listBox1.Items[i].ToString()) { musicPath[j] = "" ; } } if (listBox1.Items.Count > 0 ) { listBox1.Items.RemoveAt(i); myPlyer.currentPlaylist.removeItem(myPlyer.currentPlaylist.get_Item(i)); } if (i == listBox1.Items.Count) { listBox1.SelectedIndex = listBox1.Items.Count- 1 ; } else { listBox1.SelectedIndex = i; } } private void btnDelete_Click( object sender, EventArgs e) // 同上,并删除本地的音乐文件 { if (MessageBox.Show( " 确定要删除文件吗? " , " 提示 " , MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { int i = listBox1.SelectedIndex; string s = myPlyer.currentPlaylist.get_Item(i).sourceURL; for ( int j = 0 ; j < musicCount; j++) { if (Path.GetFileName(musicPath[j]) == listBox1.Items[i].ToString()) { musicPath[j] = "" ; } } if (listBox1.Items.Count > 0 ) { listBox1.Items.RemoveAt(i); myPlyer.currentPlaylist.removeItem(myPlyer.currentPlaylist.get_Item(i)); } if (i == listBox1.Items.Count) { listBox1.SelectedIndex = listBox1.Items.Count - 1 ; } else { listBox1.SelectedIndex = i; } try { File.Delete(s); } catch (Exception) { MessageBox.Show( " 删除文件失败! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information); } } } private void delAllToolStripMenuItem_Click( object sender, EventArgs e) // 清空播放列表 { myPlyer.Ctlcontrols.stop(); // 先停止播放器 listBox1.Items.Clear(); // 清空listbox myPlyer.currentPlaylist.clear(); // 清空播放列表 for ( int j = 0 ; j < musicCount; j++) { musicPath[j] = "" ; } } private void Form1_KeyPress( object sender, KeyPressEventArgs e) { if ( char .Equals(e.KeyChar, ' ' )) { if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying) { myPlyer.Ctlcontrols.pause(); } } } private void SToolStripMenuItem_Click( object sender, EventArgs e) // 顺序播放,默认 { myPlyer.settings.playCount = 1 ; myPlyer.currentPlaylist.clear(); for ( int j= 0 ;j<musicCount;j++) { if (musicPath[j]!= string .Empty) { myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count,myPlyer.newMedia(musicPath[j])); } } myPlyer.settings.setMode( " shuffle " , false ); myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item( 0 )); } private void ReplayToolStripMenuItem_Click( object sender, EventArgs e) // 单曲循环 { myPlyer.settings.playCount = 1000 ; } private void RandomToolStripMenuItem_Click( object sender, EventArgs e) // 随机播放,重新建立当前播放列表 { myPlyer.settings.playCount = 1 ; myPlyer.Ctlcontrols.stop(); myPlyer.currentPlaylist.clear(); Random rd = new Random(DateTime.Now.Millisecond); for ( int i = 0 ; i < 1000 ; i++) { int j = rd.Next( 0 ,musicCount - 1 ); if (musicPath[j] != string .Empty) { myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count, myPlyer.newMedia(musicPath[j])); } } myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item( 0 )); } } } (发现一个Bug,以下是修复完成后的结果) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace MusicPlayer { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string [] musicPath = new string [ 10000 ]; // 用于保存歌曲目录 int musicCount = 0 ; private void Form1_Load( object sender, EventArgs e) { myPlyer.BeginInit(); // 初始化 myPlyer.settings.autoStart = true ; // 自动播放 myPlyer.settings.setMode( " shuffle " , false ); // 顺序播放 myPlyer.settings.enableErrorDialogs = true ; myPlyer.settings.balance = 0 ; myPlyer.settings.mute = false ; myPlyer.settings.volume = 100 ; // 声音设为最大 btnBack.Enabled = false ; // 声音不对 btnForward.Enabled = false ; // 声音不对 btnBE.Enabled = false ; // 无法暂停和开始 ExitToolStripMenuItem.Enabled = false ; // 无法退出 ReplayToolStripMenuItem.Enabled = false ; // 无法单曲循环 if (File.Exists( " listbox.txt " )) // 如果存在播放列表,那么加载播放列表 { StreamReader reader = new StreamReader( " listbox.txt " ); try { while (reader.Peek() != - 1 ) { string filepath = reader.ReadLine(); if (File.Exists(filepath)) { musicPath[musicCount++] = filepath; string filename = Path.GetFileName(filepath); listBox1.Items.Add(filename); // listbox用来显示歌曲名 myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count, myPlyer.newMedia(filepath)); } } listBox1.SelectedIndex = 0 ; } catch (Exception) { listBox1.SelectedIndex = - 1 ; MessageBox.Show( " 加载播放列表失败! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { reader.Close(); } } } private void OpenToolStripMenuItem_Click( object sender, EventArgs e) // 打开音乐文件,但不加入到播放列表中 { DialogResult dr = openFileDialog1.ShowDialog(); if (dr == DialogResult.OK) { myPlyer.URL = openFileDialog1.FileName; } } private void ExitToolStripMenuItem_Click( object sender, EventArgs e) // 结束程序,但为什么不起作用?而共享此动作的btnExit却有作用? { myPlyer.Ctlcontrols.stop(); myPlyer.close(); Application.Exit(); } private void AddSingleToolStripMenuItem_Click( object sender, EventArgs e) // 添加单首歌曲到播放列表中,"添加"按钮共享此事件 { DialogResult dr = openFileDialog1.ShowDialog(); if (dr == DialogResult.OK) { string filepath = openFileDialog1.FileName; string filename = Path.GetFileName(filepath); listBox1.Items.Add(filename); musicPath[musicCount++] = filepath; myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count,myPlyer.newMedia(filepath)); } } private void AddMoreToolStripMenuItem_Click( object sender, EventArgs e) // 添加选中的文件夹中的mp3文件到播放列表中 { DialogResult dr = folderBrowserDialog1.ShowDialog(); if (dr == DialogResult.OK) { string [] filepath = Directory.GetFiles(folderBrowserDialog1.SelectedPath); foreach ( string s in filepath) { if (Path.GetExtension(s) == " .mp3 " ) { string filename = Path.GetFileName(s); listBox1.Items.Add(filename); musicPath[musicCount++] = s; myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count, myPlyer.newMedia(s)); } } } } private void listBox1_DoubleClick( object sender, EventArgs e) // 播放列表中选中的歌曲,随机播放状态下不起作用 { int j = listBox1.SelectedIndex; if (listBox1.Items.Count> 0 ) { myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(j)); } } private void Form1_FormClosing( object sender, FormClosingEventArgs e) // 退出程序的动作 { myPlyer.Ctlcontrols.stop(); myPlyer.close(); // 关闭播放器 StreamWriter writer = new StreamWriter( " listbox.txt " , false , Encoding.Unicode); // 保存播放列表 for ( int i = 0 ; i <=musicCount- 1 ; i++) { if (musicPath[i] != string .Empty) { writer.WriteLine(musicPath[i]); } } writer.Close(); } private void axWindowsMediaPlayer1_PlayStateChange( object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e) // 显示播放状态 { if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying) { label1.Text = " 正在播放 " + myPlyer.currentMedia.sourceURL+ " " +myPlyer.currentMedia.durationString; string s = myPlyer.currentMedia.sourceURL; for ( int i = 0 ; i < myPlyer.currentPlaylist.count; i++) { if (listBox1.Items[i].ToString() == Path.GetFileName(s)) { listBox1.SelectedIndex = i; break ; } } } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsBuffering) { label1.Text = " 正在缓冲 " + myPlyer.currentMedia.sourceURL; } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPaused) { label1.Text = " 暂停播放 " + myPlyer.currentMedia.sourceURL; } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsWaiting) { label1.Text = " 正在等待 " ; } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsStopped) { label1.Text = " 播放停止 " ; } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsReady) { label1.Text = " 准备就绪 " ; } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsScanForward) { label1.Text = " 正在快进 " + myPlyer.currentMedia.sourceURL; } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsScanReverse) { label1.Text = " 正在快退 " + myPlyer.currentMedia.sourceURL; } } private void btnBE_Click( object sender, EventArgs e) // 暂停/开始,不起作用 { if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying) { myPlyer.Ctlcontrols.pause(); } if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPaused) { myPlyer.Ctlcontrols.play(); } } private void btnStop_Click( object sender, EventArgs e) // 停止播放 { myPlyer.Ctlcontrols.stop(); } private void btnSlient_Click( object sender, EventArgs e) // 静音 { if (myPlyer.settings.mute == false ) { myPlyer.settings.mute = true ; } else { myPlyer.settings.mute = false ; } } private void btnBack_Click( object sender, EventArgs e) // 快退,声音不对 { if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying) { myPlyer.Ctlcontrols.fastReverse(); } } private void btnForward_Click( object sender, EventArgs e) // 快进,声音不对 { if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying) { myPlyer.Ctlcontrols.fastForward(); } } private void btnPre_Click( object sender, EventArgs e) // 上一曲 { if (listBox1.SelectedIndex != 0 ) { myPlyer.Ctlcontrols.previous(); } } private void btnNext_Click( object sender, EventArgs e) // 下一曲 { if (listBox1.SelectedIndex != listBox1.Items.Count - 1 ) { myPlyer.Ctlcontrols.next(); } } private void btnPlay_Click( object sender, EventArgs e) // 双击播放列表中选中的歌曲 { if (listBox1.Items.Count> 0 &&listBox1.SelectedIndex>= 0 ) { myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item(listBox1.SelectedIndex)); } } private void btRemove_Click( object sender, EventArgs e) // 将选中的歌曲移出播放列表 { int i = listBox1.SelectedIndex; for ( int j = 0 ; j < musicCount; j++) { if (Path.GetFileName(musicPath[j]) == listBox1.Items[i].ToString()) { musicPath[j] = "" ; } } if (listBox1.Items.Count > 0 ) { for ( int j = 0 ; j < myPlyer.currentPlaylist.count; j++) { if (listBox1.Items[i].ToString() == Path.GetFileName(myPlyer.currentPlaylist.get_Item(j).sourceURL)) myPlyer.currentPlaylist.removeItem(myPlyer.currentPlaylist.get_Item(j)); } listBox1.Items.RemoveAt(i); } if (i == listBox1.Items.Count) { listBox1.SelectedIndex = listBox1.Items.Count- 1 ; } else { listBox1.SelectedIndex = i; } } private void btnDelete_Click( object sender, EventArgs e) // 同上,并删除本地的音乐文件 { if (MessageBox.Show( " 确定要删除文件吗? " , " 提示 " , MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { int i = listBox1.SelectedIndex; string s = myPlyer.currentPlaylist.get_Item(i).sourceURL; for ( int j = 0 ; j < musicCount; j++) { if (Path.GetFileName(musicPath[j]) == listBox1.Items[i].ToString()) { musicPath[j] = "" ; } } if (listBox1.Items.Count > 0 ) { for ( int j = 0 ; j < myPlyer.currentPlaylist.count; j++) { if (listBox1.Items[i].ToString() == Path.GetFileName(myPlyer.currentPlaylist.get_Item(j).sourceURL)) myPlyer.currentPlaylist.removeItem(myPlyer.currentPlaylist.get_Item(j)); } listBox1.Items.RemoveAt(i); } if (i == listBox1.Items.Count) { listBox1.SelectedIndex = listBox1.Items.Count - 1 ; } else { listBox1.SelectedIndex = i; } try { File.Delete(s); } catch (Exception) { MessageBox.Show( " 删除文件失败! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information); } } } private void delAllToolStripMenuItem_Click( object sender, EventArgs e) // 清空播放列表 { myPlyer.Ctlcontrols.stop(); // 先停止播放器 listBox1.Items.Clear(); // 清空listbox myPlyer.currentPlaylist.clear(); // 清空播放列表 for ( int j = 0 ; j < musicCount; j++) { musicPath[j] = "" ; } } private void Form1_KeyPress( object sender, KeyPressEventArgs e) { if ( char .Equals(e.KeyChar, ' ' )) { if (myPlyer.playState == WMPLib.WMPPlayState.wmppsPlaying) { myPlyer.Ctlcontrols.pause(); } } } private void SToolStripMenuItem_Click( object sender, EventArgs e) // 顺序播放,默认 { myPlyer.settings.playCount = 1 ; myPlyer.currentPlaylist.clear(); for ( int j= 0 ;j<musicCount;j++) { if (musicPath[j]!= string .Empty) { myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count,myPlyer.newMedia(musicPath[j])); } } myPlyer.settings.setMode( " shuffle " , false ); myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item( 0 )); } private void ReplayToolStripMenuItem_Click( object sender, EventArgs e) // 单曲循环 { myPlyer.settings.playCount = 1000 ; } private void RandomToolStripMenuItem_Click( object sender, EventArgs e) // 随机播放,重新建立当前播放列表 { myPlyer.settings.playCount = 1 ; myPlyer.Ctlcontrols.stop(); myPlyer.currentPlaylist.clear(); Random rd = new Random(DateTime.Now.Millisecond); for ( int i = 0 ; i < 1000 ; i++) { int j = rd.Next( 0 ,musicCount - 1 ); if (musicPath[j] != string .Empty) { myPlyer.currentPlaylist.insertItem(myPlyer.currentPlaylist.count, myPlyer.newMedia(musicPath[j])); } } myPlyer.Ctlcontrols.playItem(myPlyer.currentPlaylist.get_Item( 0 )); } } } |
转载于:https://www.cnblogs.com/BrightMoon/archive/2013/02/23/2923328.html