音乐项目

音乐项目

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
using System.IO;
using System.Drawing.Drawing2D;

namespace _06音乐播放器项目
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }
        //SoundPlayer sound = new SoundPlayer();
        Bitmap Start = new Bitmap(20, 20);
        Bitmap Pause = new Bitmap(20, 20);
        Bitmap UpSong = new Bitmap(20, 20);
        Bitmap NextSong = new Bitmap(20, 20);
        #region 加载事件内容
        private void Form1_Load(object sender, EventArgs e)
        {
            graphics1 = panel1.CreateGraphics();
            silderBrush = new LinearGradientBrush(new PointF(0, 0), new PointF(panel1.Width,0),Color.Red,Color.Gold);
            openFileDialog1.Filter = "(请选择音乐文件)|*.wav;*.mp3";
            openFileDialog2.Filter = "(请选择图片文件)|*.jpg;*.jpeg;*.png";
            #region 注释
            //通过文件夹对象找图片
            //DirectoryInfo directory = new DirectoryInfo(paths);
            //FileInfo[] imgInfo = directory.GetFiles();
            //foreach (var item in imgInfo)
            //{
            //    string kuo = item.Extension;
            //    if (kuo==".jpg" || kuo==".png")
            //    {
            //        bgList.Add(item.Name);
            //    }
            //}
            #endregion
            pictureBox3.Image = Start;
        }
        #endregion
        #region 音乐部分内容
        //string paths = "../../Img";
        /// <summary>
        /// 装所有的每次添加的音乐的路径
        /// </summary>
        string[] roadList; 
        /// <summary>
        /// 装所有音乐的名称
        /// </summary>
        List<string> musicList = new List<string>();
        /// <summary>
        /// 装所有音乐的路径
        /// </summary>
        List<string> musicPath = new List<string>();
        private void button1_Click(object sender, EventArgs e)
        {
            //获取音乐的文件信息
            if (openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                roadList = openFileDialog1.FileNames;
                for (int i = 0; i < roadList.Length; i++)
                {
                    FileInfo info = new FileInfo(roadList[i]);
                    bool b= false;
                    if (musicList.Count>0)
                    {
                        b = musicList.Contains(info.Name);
                    }
                    if (b==false)
                    {
                        musicList.Add(info.Name);
                        listBox1.Items.Add(info.Name);
                        musicPath.Add(info.FullName);
                    }
                }
            }
            //上一曲、下一曲控件
            //foreach (var item in roadList)
            //{
            //    axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(item));
            //}
        }
        
        /// <summary>
        /// 记录当前播放的音乐索引
        /// </summary>
        int currentIndex = 0;
        private void listBox1_DoubleClick_1(object sender, EventArgs e)
        {
            //音乐开始播放
            object o = listBox1.SelectedItem;
            //sound.SoundLocation = path + "\\" + o;
            //sound.Play(); 
            currentIndex = musicList.IndexOf(o.ToString());
            PlayMusic(musicPath[currentIndex]);
            pictureBox3.Image = Pause;
        }
        string musicString = "未找到对应歌词";
        #endregion
        #region 歌词
        /// <summary>
        /// 装每首歌对应的歌词
        /// </summary>
        List<string> musicChar = new List<string>();
        List<float> musicTime = new List<float>();
        private void PlayMusic(string o)
        {
            musicChar.Clear();
            musicTime.Clear();
            if (graphics1!=null)
            {
                graphics1.Clear(Color.Purple);
            }
            axWindowsMediaPlayer1.URL = o;
            axWindowsMediaPlayer1.Ctlcontrols.play();
            int lastIndex = o.LastIndexOf(".");
            //歌词文件
            o = o.Remove(lastIndex) + ".lrc";
            //o已经是歌词文件的完整路径
            try
            {
                StreamReader reader = new StreamReader(o, Encoding.Default);
                musicString = reader.ReadToEnd();
                musicChar.AddRange(musicString.Split('\n'));
                if (musicChar.Count>0)
                {
                    foreach (var item in musicChar)
                    {
                        string[] str = item.Split('[', ']');
                        string[] timeStr = str[1].Split(':'); 
                        float times = float.Parse(timeStr[0]) * 60 + float.Parse(timeStr[1]);
                        musicTime.Add(times);
                    }
                }
            }
            catch (Exception)
            {
                musicString = "未找到对应歌词!";
            }
        }
        #endregion
        #region 文字颜色渐变 注释
        //文字颜色渐变
        private void button4_Click(object sender, EventArgs e)
        {
            //Brush brush = new LinearGradientBrush(new RectangleF(0, 0,textBox1.Width,textBox1.Height),Color.Red,Color.Blue,90);
            //Graphics graphics = textBox1.CreateGraphics();
            //graphics.DrawString("文字颜色渐变", new Font("宋体", 20, FontStyle.Bold), brush, new PointF(0, 0));
        }
        #endregion
        #region 设置窗体,移动时碰边界不会被擦除
        //设置窗体,碰边界不会被擦除
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //SetBG("zyx5.jpg");
            //Brush brush = new LinearGradientBrush(new RectangleF(0, 0, textBox1.Width, textBox1.Height), Color.Red, Color.Blue, 90);
            //Graphics graphics = textBox1.CreateGraphics();
            //graphics.DrawString("文字颜色渐变", new Font("宋体", 20, FontStyle.Bold), brush, new PointF(0, 0));
            //开始按钮
            Graphics startg = Graphics.FromImage(Start);
            startg.FillEllipse(Brushes.CadetBlue, new RectangleF(0, 0, Start.Width, Start.Height));
            //上一曲按钮
            Graphics upg = Graphics.FromImage(UpSong);
            upg.FillPolygon(Brushes.Black, new PointF[] { new PointF(0, 10), new PointF(20, 0), new PointF(20, 20) });
            pictureBox2.Image = UpSong;
            //下一曲按钮
            Graphics nextg = Graphics.FromImage(NextSong);
            nextg.FillPolygon(Brushes.Black, new PointF[] { new PointF(0, 0), new PointF(0, 20), new PointF(20, 10) });
            pictureBox4.Image = NextSong;
            //暂停按钮
            Graphics pauseg = Graphics.FromImage(Pause);
            pauseg.FillPolygon(Brushes.Red, new PointF[] { new PointF(0,0),new PointF(20,0),new PointF(20,20),new PointF(0,20)});
            //pictureBox3.Image = Start;
        }
        #endregion
        #region 设置背景图片
        //设置背景图片
        List<string> pathBG=new List<string>();
        private void button2_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog()== DialogResult.OK)
            {
                string[] imageBG = openFileDialog2.FileNames;
                for (int i = 0; i < imageBG.Length; i++)
                {
                    FileInfo info = new FileInfo(imageBG[i]);
                    pathBG.Add(info.FullName);
                }
                SetBG(pathBG[0]);
            }
        }
        private void SetBG(string file)
        {
            Image image = Image.FromFile(file);
            Bitmap bg = new Bitmap(300, 300);
            Graphics graph = Graphics.FromImage(bg);
            graph.DrawImage(image, new Rectangle(0, 0, bg.Width, bg.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
            pictureBox1.Image = bg;
        }
        /// <summary>
        /// 记录背景图片的集合
        /// </summary>
        //List<string> bgList = new List<string>();
        int bgIndex = 0;
        //更换背景图片(上一张)
        private void 上一张图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bgIndex--;
            if (bgIndex < 0)
            {
                bgIndex = pathBG.Count - 1;
            }
            SetBG(pathBG[bgIndex]);
        }
        //更换背景图片(下一张)
        private void 下一张图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bgIndex++;
            if (bgIndex >= pathBG.Count)
            {
                bgIndex = 0;
            }
            SetBG(pathBG[bgIndex]);
        }
        //切换上一曲
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            //if (graphics1 != null)
            //{
            //    graphics1.Clear(Color.Purple);
            //}
            currentIndex--;
            if (currentIndex<0)
            {
                currentIndex = musicList.Count - 1;
            }
            PlayMusic(musicPath[currentIndex]);
            listBox1.SelectedItem = musicList[currentIndex];
        }
        #endregion
        #region 切换上下一曲
        //切换下一曲
        private void pictureBox4_Click(object sender, EventArgs e)
        {
            currentIndex++;
            if (currentIndex >= musicList.Count)
            {
                currentIndex = 0;
            }
            NextMusic(currentIndex);
        }
        //上一曲、下一曲调用的方法

        private void NextMusic(int index)
        {
            if (graphics1 != null)
            {
                graphics1.Clear(Color.Purple);
            }
            
            PlayMusic(musicPath[currentIndex]);
            listBox1.SelectedItem = musicList[currentIndex];
        }
        #endregion
        #region 开始暂停按钮
        //开始暂停
        private void pictureBox3_Click(object sender, EventArgs e)
        {
            if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                axWindowsMediaPlayer1.Ctlcontrols.pause();
                pictureBox3.Image = Start;
            }
            else if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPaused)
            {
                axWindowsMediaPlayer1.Ctlcontrols.play();
                pictureBox3.Image = Pause;
            }
        }
        #endregion
        #region timer内容
        Graphics graphics1;
        Brush silderBrush;
        Graphics graphics2;
        private void timer1_Tick(object sender, EventArgs e)
        {
            float prent = 0;
            //如果一首歌播放完会自动为空
            if (axWindowsMediaPlayer1.currentMedia!=null&&graphics1!=null)
            {
                label1.Text = axWindowsMediaPlayer1.currentMedia.durationString;
                label2.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPositionString;
                //歌曲总时间
                double duration = axWindowsMediaPlayer1.currentMedia.duration;
                //当前歌曲播放的时间
                double current = axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
                
                prent = (float)current / (float)duration;
                
                //画进度条
                graphics1.FillRectangle(Brushes.Red, new RectangleF(0, 0, panel1.Width * prent, panel1.Height));

                //音乐播放时的默认音量
                //graphics2 = panel2.CreateGraphics();
                //int currents = axWindowsMediaPlayer1.settings.volume;
                //float prents = (float)currents /100;
                //graphics2.FillRectangle(silderBrush, new RectangleF(0, 0, panel2.Width * prents, panel2.Height));

                if (axWindowsMediaPlayer1.Ctlcontrols.currentPositionString!="")
                {
                    //截取播放的时间
                    string[] currentPosition = axWindowsMediaPlayer1.Ctlcontrols.currentPositionString.Split(':');
                    float nowTime = float.Parse(current.ToString("f2"));
                    if (musicTime.Count==0)
                    {
                        textBox1.Text = "未找到对应歌词!";
                    }
                    for (int i = 0; i < musicTime.Count; i++)
                    {
                        if (nowTime>=musicTime[i])
                        {
                            string[] charList = musicChar[i].Split(']');
                            if (charList[1]=="")
                            {
                                charList = musicChar[i - 1].Split(']');
                                textBox1.Text = charList[1];
                            }
                            else
                            {
                                textBox1.Text = charList[1];
                            }
                        }
                    }
                }
               
            }
            //判断自动进行下一曲
            if (prent>=0.999f)
            {
                currentIndex++;
                if (currentIndex>=musicList.Count)
                {
                    currentIndex = 0;
                }
                NextMusic(currentIndex);
            }
        }
        #endregion
        #region 进度条
        //可随时点击进度条
        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (graphics1!=null&& axWindowsMediaPlayer1.currentMedia != null)
            {
                graphics1.Clear(Color.Purple);
                graphics1.FillRectangle(Brushes.Red, new RectangleF(0, 0, e.X, panel1.Height));
                double prent = (double)e.X/panel1.Width;
                double duration = axWindowsMediaPlayer1.currentMedia.duration;
                //当前所点击的时间
                double current = duration * prent;
                axWindowsMediaPlayer1.Ctlcontrols.currentPosition = current;
            }
        }
        #endregion
        #region 音量
        //音量
        private void panel2_MouseClick(object sender, MouseEventArgs e)
        {
            //画音量
            if (axWindowsMediaPlayer1.currentMedia!=null)
            { 
                graphics2.Clear(Color.LightBlue);
                graphics2.FillRectangle(silderBrush, new RectangleF(0, 0, e.X, panel2.Height));
                double prent = (double)e.X / panel2.Width;
                double current = 100 * prent;
                axWindowsMediaPlayer1.settings.volume = Convert.ToInt32(current);
            }
        }
        private void button4_Click_1(object sender, EventArgs e)
        {
            graphics2.Clear(Color.LightBlue);
            int add = this.axWindowsMediaPlayer1.settings.volume += 5;
            ChangeVoice(add);

        }
        private void ChangeVoice(int add)
        {
            float changepro = (float)add / (float)100;
            graphics2.FillRectangle(Brushes.LightCoral, new RectangleF(0, 0, panel2.Width * changepro, panel2.Height));
            label4.Text = add.ToString();
        }


        private void button3_Click(object sender, EventArgs e)
        {
            graphics2.Clear(Color.LightBlue);
            int add = this.axWindowsMediaPlayer1.settings.volume -= 5;
            ChangeVoice(add);

        }

        #endregion


    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值