C#音乐播放器、支持在线播放

C#音乐播放器、支持在线播放_第1张图片


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using ComponentFactory.Krypton.Ribbon;
using System.Xml;
using System.IO;
using System.Net;

namespace 音乐播放器
{
    public partial class MusicPlayer : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        public MusicPlayer()
        {
            InitializeComponent();
        }
        /// 
        /// 打开本地文件
        /// 
        System.Windows.Forms.OpenFileDialog ofdLightning = new OpenFileDialog();
        /// 
        /// 播放器
        /// 
        AxWMPLib.AxWindowsMediaPlayer wmp = new AxWMPLib.AxWindowsMediaPlayer();
        /// 
        /// 当前播放曲目序号
        /// 
        private int pos;
        /// 
        /// 控制快进状态
        /// 
        private bool ReturnExecute=true;
        /// 
        /// 播放模式
        /// 单曲循环 - Single
        /// 
        private bool Single=false;
        /// 
        /// 播放模式
        /// 随机播放 - Shuffle
        /// 
        private bool Shuffle = false; 
        /// 
        /// 播放模式
        /// 循序播放 - Step
        /// 
        private bool Step = false;
        /// 
        /// 播放模式
        /// 循环播放 - Loop
        /// 
        private bool Loop = false;

        //启动状态
        //private bool Start = true;

        private void kryptonListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            //if (Start == true)
            //{
            //    this.Controls.Add(wmp);
            //    Start=false;
            //    return;
            //}
            this.Controls.Add(wmp);
            if (this.kryptonListBox1.Items.Count > 0)
            {
                if (this.kryptonListBox1.SelectedItems.Count > 0)
                {

                    
                    //wmp.URL = this.kryptonListBox1.SelectedItem.ToString();
                    //wmp.Ctlcontrols.play();
                    //this.timer1.Start();

                    pos = this.kryptonListBox1.SelectedIndex;

                    wmp.URL = this.kryptonListBox1.Items[pos].ToString();
                    wmp.Ctlcontrols.play();
                    this.timer1.Start();
                    this.kryptonLabel8.Text = this.kryptonListBox1.SelectedItem.ToString();
                    int ImageInde = ReturnImage();
                    this.pictureBox10.ImageLocation = Application.StartupPath + "\\" + ImageInde + ".jpg";
                }
            }
            else
            {
                MessageBox.Show("请选择歌曲!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

       

        private void pictureBox4_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox4.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox4_MouseLeave(object sender, EventArgs e)
        {
            this.pictureBox4.BackColor = Color.Transparent;
        }

        private void pictureBox5_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox5.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox2_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox2.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox1.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox3_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox3.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox6_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox6.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox6_MouseLeave(object sender, EventArgs e)
        {
            if (Step == true)
            {
                return;
            }
            this.pictureBox6.BackColor = Color.Transparent;
        }

        private void pictureBox3_MouseLeave(object sender, EventArgs e)
        {
            if (Single == true)
            {
                return;
            }
            this.pictureBox3.BackColor = Color.Transparent;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            if (Shuffle == true)
            {
                return;
            }
            this.pictureBox1.BackColor = Color.Transparent;
        }

        private void pictureBox2_MouseLeave(object sender, EventArgs e)
        {
            this.pictureBox2.BackColor = Color.Transparent;
        }

        private void pictureBox5_MouseLeave(object sender, EventArgs e)
        {
            this.pictureBox5.BackColor = Color.Transparent;
        }

        private void MusicPlayer_Load(object sender, EventArgs e)
        {
            int ImageInde = ReturnImage();
            this.pictureBox10.ImageLocation = Application.StartupPath + "\\" + ImageInde + ".jpg";
            wmp.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(wmp_PlaylistChange);
            this.kryptonTrackBar2.Value = 5;
        }
        //返回一个随机数
        private int ReturnImage()
        {
            Random rd = new Random();

            int str;

            int[] s = { 1,2,3,4,5,6,7,8,9,10,11,12 };

            str = s[rd.Next(12)];

            return str;
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            ofdLightning.Multiselect = true;
            ofdLightning.Filter = "可播放文件(*.mp3,*.wma,*.rm)|*.Mp3;*.wma;*.rm";
            if (ofdLightning.ShowDialog() == DialogResult.OK)
            {
                foreach (string s in ofdLightning.FileNames)
                {
                    this.kryptonListBox1.Items.Add(s.Substring(s.LastIndexOf("\\") + 1));
                    //this.kryptonListBox1.Items.Add(s);
                }
            }
        }

        private void pictureBox9_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox9.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox7_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox7.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox8_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox8.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox9_MouseLeave(object sender, EventArgs e)
        {
            this.pictureBox9.BackColor = Color.Transparent;
        }

        private void pictureBox7_MouseLeave(object sender, EventArgs e)
        {
            this.pictureBox7.BackColor = Color.Transparent;
        }

        private void pictureBox8_MouseLeave(object sender, EventArgs e)
        {
            this.pictureBox8.BackColor = Color.Transparent;
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            if (this.ofdLightning.FileName.Length > 0)
                wmp.Ctlcontrols.pause();
            else
            {
                MessageBox.Show("请选择歌曲!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void pictureBox9_Click(object sender, EventArgs e)
        {
            if (this.ofdLightning.FileName.Length > 0)
                wmp.Ctlcontrols.play();
            else
            {
                MessageBox.Show("请选择歌曲!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // 计算剩余时间   
            //double t = Math.Floor(wmp.currentMedia.duration - wmp.Ctlcontrols.currentPosition);
            int zong = (int)wmp.currentMedia.duration;
            int sheng = (int)wmp.Ctlcontrols.currentPosition;
            kryptonLabel2.Text = test(zong);
            kryptonLabel4.Text = test(sheng);

            this.kryptonTrackBar1.Maximum = zong;
            int vs=(int)wmp.Ctlcontrols.currentPosition;
            this.kryptonTrackBar1.Value = vs;
            if (wmp.settings.mute == false)
            {
                wmp.settings.volume = this.kryptonTrackBar2.Value * 10;
            }
        }
        //转化为时间格式 00;00
        private string test(int a)
        {

            int tm = (int)(a / 60);
            int tmm = a % 60;
            if (tm.ToString().Length < 2)
            {
                if (tmm.ToString().Length < 2)
                {

                    return ("0" + tm + ":0" + tmm);
                }
                return ("0" + tm + ":" + tmm);
            }
            else
            {
                return (tm + ":" + tmm);
            }

        }
        //调节音量
        private void kryptonTrackBar2_Enter(object sender, EventArgs e)
        {
            if (this.ofdLightning.FileName.Length > 0)
            {
                wmp.settings.volume = this.kryptonTrackBar2.Value * 10;
            }
            else
            {

            }
        }

        private void kryptonTrackBar2_MouseClick(object sender, MouseEventArgs e)
        {
            this.kryptonLabel6.Text = (this.kryptonTrackBar2.Value * 10).ToString();
        }

        private void kryptonTrackBar1_Click(object sender, EventArgs e)
        {
            if (this.ofdLightning.FileName.Length > 0)
            {
                wmp.Ctlcontrols.currentPosition = this.kryptonTrackBar1.Value;
            }
            else
            {

            }
        }
        private void wmp_PlaylistChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {

            switch (e.newState)
            {
                case 0:    // Undefined 
                    currentStateLabel.Text = "未知状态"; break;
                case 1:    // Stopped 
                    currentStateLabel.Text = "播放停止"; break;
                case 2:    // Paused 
                    currentStateLabel.Text = "播放暂停"; break;
                case 3:    // Playing 
                    currentStateLabel.Text = "正在播放"; break;
                case 4:    // ScanForward 
                    currentStateLabel.Text = "向前搜索"; break;
                case 5:    // ScanReverse 
                    currentStateLabel.Text = "向后搜索"; break;
                case 6:    // Buffering 
                    currentStateLabel.Text = "正在缓冲"; break;
                case 7:    // Waiting 
                    currentStateLabel.Text = "正在等待流开始"; break;
                case 8:    // MediaEnded 
                    currentStateLabel.Text = "播放流已结束"; break;
                case 9:    // Transitioning 
                    currentStateLabel.Text = "准备新的媒体文件"; break;
                case 10:   // Ready 
                    currentStateLabel.Text = "播放准备就绪"; break;
                case 11:   // Reconnecting 
                    currentStateLabel.Text = "尝试重新连接流媒体数据"; break;
                case 12:   // Last 
                    currentStateLabel.Text = "上一次状态,状态没有改变"; break;

                default:
                    currentStateLabel.Text = ("Unknown State: " + e.newState.ToString());
                    break;
            }

            if (wmp.playState == WMPLib.WMPPlayState.wmppsStopped)
            {
                pictureBox8_Click(null, null);
            }

        }
        //上一曲
        private void pictureBox7_Click(object sender, EventArgs e)
        {
            if (this.ofdLightning.FileName.Length > 0)
            {
                //int pos = this.kryptonListBox1.SelectedIndex;
                if (pos == 0)
                {
                    MessageBox.Show("已经是最后一首了");
                    return;
                }
                pos = pos - 1;
                wmp.URL = this.kryptonListBox1.Items[pos].ToString();
                wmp.Ctlcontrols.play();
                int ImageInde = ReturnImage();
                this.kryptonLabel8.Text = this.kryptonListBox1.Items[pos].ToString();
                this.pictureBox10.ImageLocation = Application.StartupPath + "\\" + ImageInde + ".jpg";
            }
            else
            {
                MessageBox.Show("请选择歌曲!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //下一曲
        private void pictureBox8_Click(object sender, EventArgs e)
        {
            if (this.ofdLightning.FileName.Length > 0)
            {
                //if (Single == true)
                //{
                //    wmp.URL = this.kryptonListBox1.Items[pos].ToString();
                //    wmp.Ctlcontrols.play();
                //    return;
                //}
                //if (Shuffle == true)
                //{
                //    return;
                //}
                //if (Step == true)
                //{
                //    return;
                //}
                //if (Loop == true)
                //{
                //    return;
                //}
                //if (pos == this.kryptonListBox1.Items.Count-1)
                //{
                //    MessageBox.Show("已经是最后一首了");
                //    return;
                //}
                if (pos == kryptonListBox1.Items.Count-1)
                {
                    MessageBox.Show("已经是最后一首了");
                    return;
                }
                pos = pos + 1;
                wmp.URL = this.kryptonListBox1.Items[pos].ToString();
                int ImageInde = ReturnImage();
                this.kryptonLabel8.Text = this.kryptonListBox1.Items[pos].ToString();
                this.pictureBox10.ImageLocation = Application.StartupPath + "\\" + ImageInde + ".jpg";
            }
            else
            {
                MessageBox.Show("请选择歌曲!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            if (this.ofdLightning.FileName.Length > 0)
            {
                if (ReturnExecute == true)
                {
                    wmp.Ctlcontrols.fastForward();
                    ReturnExecute = false;
                    pictureBox5.Image = 音乐播放器.Properties.Resources._12;
                    return;
                }
                if (ReturnExecute == false)
                {
                    wmp.Ctlcontrols.pause();
                    wmp.Ctlcontrols.play();
                    ReturnExecute = true;
                    pictureBox5.Image = 音乐播放器.Properties.Resources._8;
                    return;
                }
            }
            else
            {
                MessageBox.Show("请选择歌曲!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (this.ofdLightning.FileName.Length > 0)
                wmp.Ctlcontrols.fastReverse();
            else
            {
                MessageBox.Show("请选择歌曲!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void kryptonCheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (kryptonCheckBox1.Checked == true)
            {
                wmp.settings.mute = true;
            }
            else
            {
                wmp.settings.mute = false;
            }
        }

        private void pictureBox11_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox11.BackColor = Color.LightSteelBlue;
        }

        private void pictureBox11_MouseLeave(object sender, EventArgs e)
        {
            if (Loop == true)
            {
                return;
            }
            this.pictureBox11.BackColor = Color.Transparent;
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            Single = true;
            Shuffle=false;
            Step=false;
            Loop = false;
            Background();
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Single = false;
            Shuffle = true;
            Step = false;
            Loop = false;
            Background();
        }

        private void pictureBox11_Click(object sender, EventArgs e)
        {
            Single = false;
            Shuffle = false;
            Step = false;
            Loop = true;
            Background();
        }
        private void pictureBox6_Click(object sender, EventArgs e)
        {
            Single = false;
            Shuffle = false;
            Step = true;
            Loop = false;
            Background();
        }
        //选中背景
        private void Background()
        {
            if (Single == true)
            {
                this.pictureBox3.BackColor = Color.Blue;
            }
            else
            {
                this.pictureBox3.BackColor = Color.Transparent;
            }
            if (Shuffle == true)
            {
                this.pictureBox1.BackColor = Color.Blue;
            }
            else
            {
                this.pictureBox1.BackColor = Color.Transparent;
            }
            if (Step == true)
            {
                this.pictureBox6.BackColor = Color.Blue;
            }
            else
            {
                this.pictureBox6.BackColor = Color.Transparent;
            }
            if (Loop == true)
            {
                this.pictureBox11.BackColor = Color.Blue;
            }
            else
            {
                this.pictureBox11.BackColor = Color.Transparent;
            }
        }


        private void kryptonTrackBar1_Scroll(object sender, EventArgs e)
        {
            if (this.ofdLightning.FileName.Length > 0)
            {
                wmp.Ctlcontrols.currentPosition = this.kryptonTrackBar1.Value;
            }
            else
            {

            }
        }

        public string GetMP3URL(string fString)
        {
            try
            {
                string strAPI = "http://box.zhangmen.baidu.com/x?op=12&count=1&title=";

                //http://box.zhangmen.baidu.com/x?op=12&count=1&title=My%20love$$ss$$$$
                
                string[] Fstrings=fString.Split('-');
                if (Fstrings.Length == 2)
                {

                    strAPI = strAPI + Fstrings[0] + "$$" + Fstrings [1]+ "$$$$";
                }
                else
                {
                    strAPI = strAPI + Fstrings[0] + "$$";
                }


                XmlTextReader hfXMLReader = new XmlTextReader(strAPI);

                DataSet ds = new DataSet();

                ds.ReadXml(hfXMLReader);

                string strDecode = ds.Tables["url"].Rows[0]["decode"].ToString().Replace("\n", "");          //读取歌曲名称

                string strEncode = ds.Tables["URL"].Rows[0]["encode"].ToString().Replace("\n", "");        //读取歌曲编码

                //string strLrc = ds.Tables["URL"].Rows[0]["type"].ToString().Replace("
", ""); //读取歌词ID string strLrc = ds.Tables["URL"].Rows[0]["lrcid"].ToString().Replace("
", ""); //读取歌词ID string strPath = ""; string strExt = ""; string[] strPre = strEncode.Split("/".ToCharArray()); strPath = strEncode.Replace(strPre[strPre.Length - 1], strDecode); //赋值MP3真正地址 string strLrcPath = "http://box.zhangmen.baidu.com/bdlrc/"; //歌词基本地址 if (strLrc == "0") { strLrc = "暂无歌词"; } else { strLrc = strLrcPath + (Int32.Parse(strLrc) / 100).ToString() + "/" + strLrc + ".lrc"; } switch (ds.Tables["url"].Rows[0]["type"].ToString()) { case "1": strExt = "rm"; break; case "0": strExt = "mp3"; break; case "2": strExt = "wma"; break; } if (strEncode == "nothing") { return "nothing"; } return strEncode + " " + strDecode + " " + strExt + " " + strLrc + " " + strPath; } catch { return "nothing"; } } private void kryptonButton2_Click(object sender, EventArgs e) { try { string strEncode = ""; string strDecode = ""; string strLrc = ""; string strExt = ""; string strPath = ""; string[] AppString; string AppAPI = GetMP3URL(kryptonTextBox1.Text); if (AppAPI == "nothing") { MessageBox.Show("找不到音乐 " + kryptonTextBox1.Text + " 请更换查询名称"); return; } else { AppString = AppAPI.Split(" ".ToCharArray()); strEncode = AppString[0].ToString(); //编码 strDecode = AppString[1].ToString(); //解码 strExt = AppString[2].ToString(); //扩展名 strLrc = AppString[3].ToString(); //歌词URL strPath = AppString[4].ToString(); //歌曲URL this.Controls.Add(wmp); wmp.URL = strPath; kryptonLabel8.Text = kryptonTextBox1.Text; label1.Text = getWebContent(strLrc); int ImageInde = ReturnImage(); this.pictureBox10.ImageLocation = Application.StartupPath + "\\" + ImageInde + ".jpg"; } } catch { MessageBox.Show("找不到音乐 " + kryptonTextBox1.Text + " 请更换查询名称"); } } private string getWebContent(string url) { try { StringBuilder sb = new StringBuilder(""); WebRequest request = WebRequest.Create(url); request.Timeout = 10000;//10秒请求超时 StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.GetEncoding("GB2312")); while (sr.Peek() >= 0) { sb.Append(sr.ReadLine()); } return sb.ToString(); } catch (WebException ex) { return ex.Message; } } } }


你可能感兴趣的:(C#)