c#音樂播放

程式猿丶菜鳥發表於2020-11-19

C#製作簡單的音樂播放,播放暫停上一首下一首以及歌詞顯示基本可以實現。

在這裡插入圖片描述

public partial class Form2 : Form
    {
        WindowsMediaPlayer wm = new WindowsMediaPlayer();
        private int count = 0;
        private int num = 0;//下拉選單索引
        private int flag =0;//判斷當前歌曲的標誌
        private double max, min;//進度條最大值和最小值
        private int index = 1;//背景圖片改變的索引
        List<string> list1 = new List<string>();//存放歌詞時間的集合
        List<string> list = new List<string>();//存放歌詞的集合
        private String times;
        public Form2()
        {
            InitializeComponent();
            getLysic();
        }
        //播放/暫停鍵設定
        private void button2_Click(object sender, EventArgs e)
        {
            String name = button2.Text;
            //點選後更換圖片並播放音樂
            if (count==0&&name.Equals("播放"))
            {
                //更換圖片
                button2.Text = "暫停";
                button2.Image = Image.FromFile(@"D:\database\picture\10.png");
                //獲取當前下拉選單內容
                String musicName = toolStripComboBox1.Text;
                wm.URL = @"D:\database\music\" + musicName + ".mp3";
                count = 1;
            }
            else {
                if (name.Equals("播放"))
                {
                    wm.controls.play();
                    button2.Text = "暫停";
                    button2.Image = 			Image.FromFile(@"D:\database\picture\10.png");
                }
                else {
                    wm.controls.pause();
                    button2.Text = "播放";
                    button2.Image = 				      Image.FromFile(@"D:\database\picture\08.png");
                }
            }
            timer1.Enabled = true;
        }
        //下拉選單設定
        private void toolStripComboBox1_TextChanged(object sender, EventArgs e)
        {
            list1.Clear();
            list.Clear();
            getLysic();
            wm.controls.stop();
            String name = toolStripComboBox1.Text;
            if (!toolStripComboBox1.Items.Contains(name))
            {
                //彈窗有點問題 wait修改
                DialogResult dr = MessageBox.Show("沒有找到該音樂", "提示資訊", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                toolStripComboBox1.Text = "告白氣球 - 周杰倫";
            }
            else
            {
                wm.URL = @"D:\database\music\" + name + ".mp3";
                index++;
                if (index == 5)
                {
                    index = 0;
                }
                else {
                    panel1.BackgroundImage = Image.FromFile(@"D:\database\picture\zjl0"+index+".png");
                }
                //得到當前索引值
                num = toolStripComboBox1.Items.IndexOf(name);
                //Console.WriteLine(num);
                button2.Text = "暫停";
                button2.Image = Image.FromFile(@"D:\database\picture\10.png");
            }
        }
        //上一首設定
        private void button1_Click(object sender, EventArgs e)
        {
            num--;
            if (num == -1)
            {
                num = toolStripComboBox1.Items.Count - 1;
                //Console.WriteLine(num);
            }
            flag = 0;
            foreach (String item in toolStripComboBox1.Items)
            {
                if (flag == num)
                {
                    toolStripComboBox1.Text = item;
                    toolStripButton1.Text = "播放";
                    break;
                }
                flag++;
            }
        }
        //下一首設定
        private void button3_Click(object sender, EventArgs e)
        {
            num++;
            if (num == toolStripComboBox1.Items.Count)
            {
                num = 0;
            }
            flag = 0;
            foreach (String item in toolStripComboBox1.Items)
            {
                if (flag == num)
                {
                    toolStripComboBox1.Text = item;
                    toolStripButton1.Text = "播放";
                    break;
                }
                flag++;
            }
        }
        //滑鼠按下
        private void trackBar1_MouseDown(object sender, MouseEventArgs e)
        {
            //獲取播放時的位置
            timer1.Enabled = false;//計時器停止
            wm.controls.pause();
        }
        //滑鼠抬起
        private void trackBar1_MouseUp(object sender, MouseEventArgs e)
        {
            //獲取拖動後的位置
            double value = trackBar1.Value;
            wm.controls.currentPosition = value;
            wm.controls.play();
            timer1.Enabled = true;//計時器停止
        }
        //計時器 自動播放下一首
        private void timer1_Tick(object sender, EventArgs e)
        {
            //步驟 判斷當前歌曲是否已經播完
            if (wm.playState == WMPPlayState.wmppsStopped)
            {
                num++;
                if (num == toolStripComboBox1.Items.Count)
                {
                    num = 0;
                }
                flag = 0;
                foreach (String item in toolStripComboBox1.Items)
                {
                    if (flag == num)
                    {
                        toolStripComboBox1.Text = item;
                        toolStripButton1.Text = "播放";
                        break;
                    }
                    flag++;
                }
            }
            //進度條的改變
            //獲取音樂的長度
            max = wm.currentMedia.duration;
            //獲取音樂播放的位置
            min = wm.controls.currentPosition;
            trackBar1.Maximum = (int)max;
            trackBar1.Value = (int)min;
            label1.Text = wm.controls.currentPositionString+"/"+ wm.currentMedia.durationString+
                " "+wm.currentMedia.name;
            //程式執行需要時間無法匹配到毫秒值
            //times = wm.controls.currentPosition.ToString("0.00").Split('.');
            //Console.WriteLine(wm.controls.currentPosition.ToString("0.00"));
            times = wm.controls.currentPositionString; //+ "." + times[1];
            for (int i = 0; i < list1.Count; i++) {
                //Console.WriteLine(times);
                if (list1[i].Equals(times)) {
                    label2.Text =list[i];
                }
            }
        }
        //獲取歌詞
        private void getLysic() {
            String name = toolStripComboBox1.Text;
            FileStream fs = new FileStream(@"D:\database\music\"+name+".txt", FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            String len = null;
            String[] time;
            while ((len=sr.ReadLine())!=null) {
                time = len.Split(']');
                if (time != null) {
                    time[0] = time[0].Substring(1,5);
                }
                list1.Add(time[0]);
                list.Add(time[1]);
            }
        }
    }
}
如有需要檔案可以聯絡qq:2426338156 第一次寫,有很多不足,還請諒解。

相關文章