• 播放器项目 用泛型集合实现 歌词显示功能


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace 播放器项目B
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                musicPlayer.Ctlcontrols.play();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                musicPlayer.Ctlcontrols.pause();
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                musicPlayer.Ctlcontrols.stop();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {            
                //在程序加载的时候,取消播放器的自动播放功能
                musicPlayer.settings.autoStart = false;
                musicPlayer.URL = @"";//自定义歌曲的路径
                label1.Image = Image.FromFile(@"");
            }
            bool b = true;//这个布尔值,用于控制记忆播放位置
            private void btnPlayorPause_Click(object sender, EventArgs e)
            {
                if(btnPlayorPause.Text =="播放")
                {
                    if (b) //布尔值判断
                    { 
                    //获得选中的歌曲 让音乐从头播放
                    musicPlayer.URL = listPath[listBox1.SelectedIndex];
                    }
                    musicPlayer.Ctlcontrols.play();
                    btnPlayorPause.Text = "暂停";
    
                    IsExistLrc(listPath[listBox1.SelectedIndex]);//歌词显示判断,调用歌词方法
                }
                else if(btnPlayorPause.Text=="暂停")
                {
                    musicPlayer.Ctlcontrols.pause();
                    btnPlayorPause.Text = "播放";
                    b = false;
                }
            }
            //声明一个泛型集合,用来存储音乐文件的全路径
            List<string> listPath = new List<string>();
            private void button4_Click(object sender, EventArgs e)
            {
                //打开对话框,选择音乐
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.InitialDirectory = @"D:";
                ofd.Filter = "音乐文件|*.wav|MP3文件|*.mp3|所有文件|*.*";
                ofd.Title = "请选择音乐文件哦亲(づ ̄3 ̄)づ╭❤~";
                ofd.Multiselect = true;
                ofd.ShowDialog();
    
                //获得在文本框中选择文件的全路径
                string[] path = ofd.FileNames;
                for (int i = 0; i < path.Length; i++)
                {
                    //将音乐文件的全路径存储到泛型集合中
                    listPath.Add(path[i]);
                    //将音乐文件的文件名存储到ListBox中
                    listBox1.Items.Add( Path.GetFileName(path[i]));
                }
            }
    
            private void listBox1_DoubleClick(object sender, EventArgs e)
            {
                if(listBox1.Items.Count == 0)
                {
                    MessageBox.Show("请首先选择音乐文件");
                    return;
                }
                try { 
                //双击播放对应的音乐
                musicPlayer.URL = listPath[listBox1.SelectedIndex];//集合里面一一对应
                musicPlayer.Ctlcontrols.play();
                btnPlayorPause.Text = "暂停";
                    //歌词显示判断,调用歌词方法
                IsExistLrc(listPath[listBox1.SelectedIndex]);
                    }
                catch { }
            }
    
            private void button6_Click(object sender, EventArgs e)
            {
                
                //获得当前选中项的索引
                int index = listBox1.SelectedIndex;
                //清空所有选中项的索引
                listBox1.SelectedIndices.Clear();
    
                index++;
                if(index==listBox1.Items.Count)
                {
                    index=0;
                }
                //将改变后的索引重新赋值给当前选中项的索引
                listBox1.SelectedIndex = index;
                musicPlayer.URL=listPath[index];
                musicPlayer.Ctlcontrols.play();
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                //获得当前选中项的索引
                int index = listBox1.SelectedIndex;
                //清空所有选中项的索引
                listBox1.SelectedIndices.Clear();
    
                index--;
                if (index < 0)
                {
                    index = listBox1.Items.Count-1;
                }
                //将改变后的索引重新赋值给当前选中项的索引
                listBox1.SelectedIndex = index;
                musicPlayer.URL = listPath[index];
                musicPlayer.Ctlcontrols.play();
            }
    
            private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                //点击删除选中项目,listPath中也要删除,listBox1.Item也要删
                //先删除集合
                int count = listBox1.SelectedItems.Count;
                for (int i = 0; i < count; i++)
                {
                    listPath.RemoveAt(listBox1.SelectedIndex);
                    listBox1.Items.RemoveAt(listBox1.SelectedIndex);
                }
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
                //点击放音或静音
                if(label1.Tag.ToString()=="1")
                {
                    //目的:让你静音
                    musicPlayer.settings.mute = true;
                    //显示静音的图片
                    label1.Image = Image.FromFile(@"静音.jpg");
                    label1.Tag = 2;
                }
                else if(label1.Tag.ToString()=="2")
                {
                    musicPlayer.settings.mute = false;//放音
                    //显示放音的图片
                    label1.Image = Image.FromFile(@"放音.jpg");
                    label1.Tag = 1;
                }
            }
    
            
            //歌词显示功能
            void IsExistLrc(string songPath)
            {
                //每次新播放歌词之前必须,清空两个集合的内容
                listLrcText.Clear();
                listTime.Clear();
    
               songPath += ".lrc";//歌词文件名
               if( File.Exists(songPath))
               {
                   //读取歌词文件
                   string[] lrcText = File.ReadAllLines(songPath,Encoding.Default);
                   //格式化歌词
                   FormatLrc(lrcText);
               }
               else
               {
                   label2.Text = "歌词未找到";
               }
            }
            //声明集合存储时间
            List<double> listTime = new List<double>();
            //存储歌词
            List<string> listLrcText = new List<string>();
            void FormatLrc(string[] lrcText)//格式化歌词
            {
                for (int i = 0; i < lrcText.Length; i++)
                {
                    //File.ReadAllLines(songPath)
                    //[00:15.57]当我和世界不一样
                    string[] lrcTemp = lrcText[i].Split(new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries);
                    //00:15.57   lrcTemp[0]
                    //当我和世界不一样 lrcTemp[1]
                    string[] lrcNewTemp = lrcTemp[0].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    //00 lrcNewTemp[0]
                    //15.57 lrcNewTemp[1]
                    double time = double.Parse(lrcNewTemp[0]) * 60 + double.Parse(lrcNewTemp[1]);
                    //截取出来的歌词时间加入泛型集合中
                    listTime.Add(time);
                    //截取出来的歌词加入泛型集合中
                    listLrcText.Add(lrcTemp[1]);
                }
            }
    
            private void timer1_Tick(object sender, EventArgs e)//判断自动下一曲
            {
                //如果播放器的状态==正在播放中
                if(musicPlayer.playState==WMPLib.WMPPlayState.wmppsPlaying)
                {
                    lblInformation.Text = musicPlayer.currentMedia.duration.ToString() + "
    " + musicPlayer.currentMedia.durationString + "
    " + musicPlayer.Ctlcontrols.currentPosition.ToString() + "
    " + musicPlayer.Ctlcontrols.currentPositionString;
                   // lblInformation.Text = musicPlayer.currentMedia.duration.ToString() + "
    " + musicPlayer.currentMedia.durationString + "
    " + musicPlayer.Ctlcontrols.currentPosition.ToString() + "
    " + musicPlayer.Ctlcontrols.currentPositionString;
                    double d1 = double.Parse(musicPlayer.currentMedia.duration.ToString());//总时间
                    double d2 = double.Parse(musicPlayer.Ctlcontrols.currentPosition.ToString())+1;//当前时间
                    
                    //if (musicPlayer.currentMedia.durationString == musicPlayer.Ctlcontrols.currentPositionString)
                    if(d1<= d2)
                    {
                        //获得当前选中项的索引
                        int index = listBox1.SelectedIndex;
    
                        //清空所有选中项的索引
                        listBox1.SelectedIndices.Clear();
                        index++;
                        if (index == listBox1.Items.Count)
                        {
                            index = 0;
                        }
                        //将改变后的索引重新的赋值给当前选中项的索引
                        listBox1.SelectedIndex = index;
                        musicPlayer.URL = listPath[index];
                        musicPlayer.Ctlcontrols.play();
                    }
                }           
            }
            private void timer2_Tick(object sender, EventArgs e)//歌词显示
            {
                for (int i = 0; i < listTime.Count; i++)
                {
                    if (musicPlayer.Ctlcontrols.currentPosition >= listTime[i] && musicPlayer.Ctlcontrols.currentPosition < listTime[i+1])
                    {
                        label2.Text = listLrcText[i];
                    }
                }            
            }
    
            private void button7_Click(object sender, EventArgs e)
            {
                musicPlayer.settings.volume += 5;//音量+
            }
    
            private void button8_Click(object sender, EventArgs e)
            {
                musicPlayer.settings.volume -= 5;//音量-
            }
    
    
            
        }
    }
  • 相关阅读:
    游遍中国 —— 大街小巷、秘境与远方
    Batch Normalization 反向传播(backpropagation )公式的推导
    Batch Normalization 反向传播(backpropagation )公式的推导
    【社会/人文】概念的理解 —— 断舍离、饭(饭制版)
    【社会/人文】概念的理解 —— 断舍离、饭(饭制版)
    linux 线程的同步 一 (互斥量和信号量)
    linux find
    正则表达式30分钟入门教程-新
    linux sed命令详解 *****
    linux vim tutor
  • 原文地址:https://www.cnblogs.com/blacop/p/5994678.html
Copyright © 2020-2023  润新知