黑马程序员windows播放器

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 media
{
    public enum State
    {
        //单曲循环
        singlePlay=0,
        //无序播放
        unorderLoopPlay=1,
        //列表循环
        listPlay=2

    }
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog browser = new FolderBrowserDialog();

            DialogResult diares = browser.ShowDialog();

            if (diares == DialogResult.OK)
            {

                string PathName = browser.SelectedPath;

                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(PathName);

                foreach (System.IO.FileInfo f in dir.GetFiles())
                {

                    if (f.Extension == ".mp3" || f.Extension == ".mp4" || f.Extension == ".avi" || f.Extension == ".wmv" || f.Extension == ".3GP" || f.Extension == ".flv")
                    {

                        PlayList.Items.Add(f.FullName);

                    }

                }

            }


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PlayList.Items.Clear();
        
        }
        //添加歌曲
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog songfile = new OpenFileDialog();
           

            songfile.Multiselect = true;

            songfile.Filter = "可播放的歌曲文件|*.rmvb;*.mp3;*.mp4;*.avi;*.wmv;*.3GP;*.flv|所有文件*.*|*.*";

            DialogResult diares = songfile.ShowDialog();

            if (diares == DialogResult.OK)
            {

                foreach (string f in songfile.FileNames)
                {
                    PlayList.Items.Add(f);
                }

            }

        }
        //清空列表
        private void button3_Click(object sender, EventArgs e)
        {
            PlayList.Items.Clear();
        }

        //双击播放列表里的播放文件后播放
        private void PlayList_DoubleClick(object sender, EventArgs e)
        {
            if (PlayList.SelectedIndex >=0)
            {
                //axWindowsMediaPlayer1.URL = PlayList.SelectedItem.ToString();
                axWindowsMediaPlayer1.URL = PlayList.Items[PlayList.SelectedIndex].ToString();
                this.Text = "我的播放器" + PlayList.SelectedItem.ToString();
            }
            else
            {
                MessageBox.Show("请选中一首歌曲");
            }
        }
        //下一曲
        private void btnNext_Click(object sender, EventArgs e)
        {
            Next();
        }
        //上一曲
        private void btnPrew_Click(object sender, EventArgs e)
        {
            if (this.PlayList.SelectedIndex == 0)
            {
                axWindowsMediaPlayer1.URL = this.PlayList.Items[PlayList.SelectedIndex].ToString();
              
                this.Text = "我的播放器" + PlayList.Items[PlayList.SelectedIndex ].ToString();

            }
            else if (this.PlayList.SelectedIndex > 0)
            {
                axWindowsMediaPlayer1.URL = this.PlayList.Items[PlayList.SelectedIndex - 1].ToString();

                this.Text = "我的播放器" + PlayList.Items[PlayList.SelectedIndex - 1].ToString();
                this.PlayList.SelectedIndex -= 1;
            }
         
        }
 
        //下一曲方法
        public void Next()
        {
            if (PlayList.SelectedIndex < this.PlayList.Items.Count - 1)
            {
                axWindowsMediaPlayer1.URL = this.PlayList.Items[PlayList.SelectedIndex + 1].ToString();
                //axWindowsMediaPlayer1.URL = (PlayList.SelectedValue + 1).ToString();


                this.Text = "我的播放器" + PlayList.Items[PlayList.SelectedIndex + 1].ToString();
                this.PlayList.SelectedIndex += 1;
              
            }
            else
            {
                //最后一首歌曲,下一首从第一首开始
                axWindowsMediaPlayer1.URL = this.PlayList.Items[0].ToString();
                this.Text = "我的播放器" + PlayList.Items[0].ToString();
            }
        }
        //自动播放
        //记录当前状态
        int state = -1;
        private void timer1_Tick(object sender, EventArgs e)
        {
                if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsStopped)
                {
                    if(state==0)
                    {
                        //单曲循环
                        this.axWindowsMediaPlayer1.URL = this.PlayList.Items[this.PlayList.SelectedIndex].ToString();
                        this.Text = "我的播放器" + PlayList.Items[PlayList.SelectedIndex].ToString();
                    }
                    else if (state == 1)
                    {
                        //无序播放
                        Random rd = new Random();
                        int random = rd.Next(0, this.PlayList.Items.Count-1);
                        this.axWindowsMediaPlayer1.URL = this.PlayList.Items[random].ToString();
                        this.PlayList.SelectedIndex = random;
                        this.Text = "我的播放器" + PlayList.Items[random].ToString();
                    }
                    else
                    {
                        //自动播放与列表循环
                        Next();
                    }

                }
          
        }
        //单曲循环
        private void btnloop_Click(object sender, EventArgs e)
        {
            state = 0;
        }

        //列表循环
        private void btnlistloop_Click(object sender, EventArgs e)
        {
            state = 2;  
        }
        //无序播放
        private void btnwuxu_Click(object sender, EventArgs e)
        {
            state = 1;
        }

    }
}

原文地址:https://www.cnblogs.com/javawebsoa/p/3065393.html