A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xiaqingchao 中级黑马   /  2013-2-20 18:39  /  1680 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


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;
namespace 播放器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            MusicPlayer.settings.autoStart = false;
            //MusicPlayer.URL = @"C:\KuGou\Coyote Ugly - Can't Fight The Moonlight.mp3";
            //lsbliebiao.Items.Add("Can't Fight The Moonlight.mp3");
            //list.Add(@"C:\KuGou\Coyote Ugly - Can't Fight The Moonlight.mp3");
            //lsbliebiao.SelectedIndex = 0;
            btnplay.Enabled = false;
            stop.Enabled = false;
            btnNext.Enabled = false;
            btnUp.Enabled = false;
            trackBar1.Value = 10;
            MusicPlayer.settings.volume = 10;
        }
        bool falg = true;
        List<string> list = new List<string>();
        List<string> listlrc = new List<string>();
        List<string> list1 = new List<string>();
        List<string> list2 = new List<string>();
        private void btnplay_Click(object sender, EventArgs e)
        {
            if (falg)
            {
                int index = lsbliebiao.SelectedIndex;
                if (index != lsbliebiao.SelectedIndex)
                {
                    MusicPlayer.URL = list[index];
                }
                MusicPlayer.Ctlcontrols.play();
                btnplay.Text = "停止";
                falg = false;
            }
            else
            {
                MusicPlayer.Ctlcontrols.pause();
                btnplay.Text = "播放";
                falg = true;
            }
        }
        private void stop_Click(object sender, EventArgs e)
        {
            MusicPlayer.Ctlcontrols.stop();
            btnplay.Text = "播放";
            falg = true;
            lblsong.Text = "歌词";
        }
        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择音乐";
            ofd.Multiselect = true;
            ofd.Filter = "(*.mp3)|*.mp3";
            ofd.InitialDirectory = @"C:\KuGou";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string[] fileName = ofd.FileNames;
                for (int i = 0; i < fileName.Length; i++)
                {
                    string name = fileName.Substring(fileName.LastIndexOf('\\') + 1);
                    lsbliebiao.Items.Add(name);
                    list.Add(fileName);
                    string strLrc = fileName.Substring(0,fileName.LastIndexOf ('.'))+".lrc";
                    if (File.Exists(strLrc))
                    {
                        list2.Add(strLrc);
                        //listlrc.AddRange(File.ReadAllLines(strLrc, Encoding.UTF8));
                    }
                    else
                    {
                        lblsong.Text = "未找到歌词";
                    }
                  
                }
               
                lsbliebiao.SelectedIndex = list.Count - 1;
                MusicPlayer.URL = list[list.Count-1];
               //list1 = FormatLrc();
                btnplay.Text = "停止";
                falg = false;
                MusicPlayer.Ctlcontrols.play();
                btnplay.Enabled = true;
                stop.Enabled = true;
                btnNext.Enabled = true;
                btnUp.Enabled = true;
            }
        }
        private List<string> FormatLrc()
        {
            List <string> listsonglrc = new List<string> ();
            for (int i = 0; i < listlrc.Count; i++)
            {
                string[] lrc1 = listlrc .Split (new char []{'[',']'}, StringSplitOptions.RemoveEmptyEntries );
                if (lrc1.Length > 1)
                {
                    for (int j = 0; j < lrc1.Length-1;j++ )
                    {
                        listsonglrc.Add(lrc1[j]+'|'+lrc1[lrc1.Length -1]);
                    }
                }
            }
            for (int i = 0; i < listsonglrc.Count; i++)
            {
                for (int j = 0; j < listsonglrc.Count - 1 - i; j++)
                {
                    if (string .Compare (listsonglrc[j ],listsonglrc [j +1])>0)
                    {
                        string temp = listsonglrc[j];
                        listsonglrc[j] = listsonglrc[j + 1];
                        listsonglrc[j + 1] = temp;
                    }
                }
            }
            return listsonglrc;         
        }

        private void lsbliebiao_MouseDoubleClick(object sender, MouseEventArgs e)
        {
           
            if (lsbliebiao.Items.Count > 0)
            {
                int index = lsbliebiao.SelectedIndex;
                MusicPlayer.URL = list[index];
                MusicPlayer.Ctlcontrols.play();
                btnplay.Text = "停止";
                falg = false;
            }
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            int index = lsbliebiao.SelectedIndex;
            index++;
            if (index == lsbliebiao.Items.Count)
            {
                index = 0;
            }
            lsbliebiao.SelectedIndex = index;
            MusicPlayer.URL = list[index];
            MusicPlayer.Ctlcontrols.play();
            btnplay.Text = "停止";
           
        }
        private void btnUp_Click(object sender, EventArgs e)
        {
            int index = lsbliebiao.SelectedIndex;
            index--;
            if (index < 0)
            {
                index = lsbliebiao.Items.Count - 1;
            }
            lsbliebiao.SelectedIndex = index;
            MusicPlayer.URL = list[index];
            MusicPlayer.Ctlcontrols.play();
            btnplay.Text = "停止";
        }
        private void btnMu_Click(object sender, EventArgs e)
        {
            if (btnMu.Text == "静音")
            {
                MusicPlayer.settings.mute = true;
                btnMu.Text = "取消静音";
            }
            else
            {
                MusicPlayer.settings.mute = false;
                btnMu.Text ="静音";
            }
        }
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            MusicPlayer.settings.volume = trackBar1.Value;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (MusicPlayer.currentMedia != null)
            {
                lbltime.Text = MusicPlayer.Ctlcontrols.currentPositionString;
                lblname.Text = MusicPlayer.currentMedia.name;
            }
            if (list1 .Count >0&&MusicPlayer.currentMedia !=null )
            {
                string time = MusicPlayer.Ctlcontrols.currentPositionString;
                for (int i = 0; i < list1.Count-1;i++)
                {
                    if (string.Compare(list1, time) < 0 && string.Compare(time, list1[i + 1]) < 0)
                    {
                        lblsong.Text = list1.Split('|')[1];
                    }
                }
            }

        }
        private void MusicPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            if (list2.Count>0)
            {
                listlrc.Clear();
                listlrc.AddRange(File.ReadAllLines(list2[lsbliebiao.SelectedIndex], Encoding.UTF8));
                list1.Clear();
            }
            else
            {
                list1.Clear();
            }
            list1 = FormatLrc();
            if (MusicPlayer.playState ==WMPLib.WMPPlayState .wmppsMediaEnded)
            {
                int index = lsbliebiao.SelectedIndex;
                index++;
                if (index == lsbliebiao.Items.Count)
                {
                    index = 0;
                }
                lsbliebiao.SelectedIndex = index;
                MusicPlayer.URL = list[index];
               
            }
            if (MusicPlayer.playState == WMPLib.WMPPlayState.wmppsReady)
            {
                try
                {
                  
                    MusicPlayer.Ctlcontrols.play();
                }
                catch
                { }
            }
        }
      
    }
}

评分

参与人数 1黑马币 +1 收起 理由
汪振 + 1 很给力!

查看全部评分

3 个回复

倒序浏览
= =估计没分加。。。因为没注释。。。亲,标记注释是一项很好很好很好的习惯啊- -
回复 使用道具 举报
很牛的样子

回复 使用道具 举报
打包一下,可以下载赚金币的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马