09通过winfrom实现简单的播放音、视频
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.IO; 10 11 namespace musicPlayer 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 List<string> listSong = new List<string>(); 20 /// <summary> 21 /// 实现按钮1的打开功能 22 /// </summary> 23 /// <param name="sender"></param> 24 /// <param name="e"></param> 25 private void button1_Click(object sender, EventArgs e) 26 { 27 OpenFileDialog open = new OpenFileDialog(); 28 open.Title = "请选择音乐文件"; 29 open.InitialDirectory = @"E:\"; 30 open.Multiselect = true; 31 open.Filter = "所有文件|*.*|mp3文件|*.mp3"; 32 open.ShowDialog(); 33 string[] path = open.FileNames; 34 for (int i = 0; i < path.Length; i++) 35 { 36 listBox1.Items.Add(Path.GetFileName(path[i])); 37 listSong.Add(path[i]); 38 } 39 40 } 41 /// <summary> 42 /// 把文件显示在listbox中 43 /// </summary> 44 /// <param name="sender"></param> 45 /// <param name="e"></param> 46 private void listBox1_DoubleClick(object sender, EventArgs e) 47 { 48 //调用静态字段并实现双击播放 49 axWindowsMediaPlayer1.URL = listSong[listBox1.SelectedIndex]; 50 51 } 52 /// <summary> 53 /// 上一个文件并播放 54 /// </summary> 55 /// <param name="sender"></param> 56 /// <param name="e"></param> 57 private void button2_Click(object sender, EventArgs e) 58 { 59 60 int index = listBox1.SelectedIndex; 61 index--; 62 if (index <0) 63 { 64 index = listBox1.Items.Count-1; 65 } 66 67 listBox1.SelectedIndex = index; 68 axWindowsMediaPlayer1.URL = listSong[index]; 69 70 71 } 72 /// <summary> 73 /// 下一个 74 /// </summary> 75 /// <param name="sender"></param> 76 /// <param name="e"></param> 77 private void button3_Click(object sender, EventArgs e) 78 { 79 int index = listBox1.SelectedIndex; 80 index++; 81 if (index == listBox1.Items.Count) 82 { 83 index = 0; 84 } 85 86 listBox1.SelectedIndex = index; 87 axWindowsMediaPlayer1.URL = listSong[index]; 88 } 89 /// <summary> 90 /// 当无操作时重复播放 91 /// </summary> 92 /// <param name="sender"></param> 93 /// <param name="e"></param> 94 private void axWindowsMediaPlayer1_StatusChange(object sender, EventArgs e) 95 { 96 //判断视频是否已停止播放 97 if ((int)axWindowsMediaPlayer1.playState == 1) 98 { 99 //停顿2秒钟再重新播放 100 System.Threading.Thread.Sleep(2000); 101 102 //axWindowsMediaPlayer1.Ctlcontrols.next(); 103 //重新播放 104 axWindowsMediaPlayer1.Ctlcontrols.play(); 105 } 106 107 } 108 } 109 }
过完年没想到忽然事情很多,结果搁置了这么久,还好比葫芦画瓢的本领倒是没减退。
这几天在看传智的视频,本次代码的实现通过结合视频中的讲述以及网上查找资料得以完成,但是仍有很多不满意的地方。
譬如一首歌曲停止后无法自动播放下一首,目前考虑可能是调用的是系统播放器,而播放器的真实列表中并没有其他文件,故无法自动播放下一首,只能单曲循环.....
暂且放下,留待日后寻求解决。听一些前辈说,死钻牛角尖并不是良好的学习态度。
路漫漫其修远兮 吾将上下而求索