音視頻播放器源碼




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 WinMediaPlayer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void btnPlay_Click(object sender, EventArgs e)
        {
            MusicPlayer.Ctlcontrols.play();
        }


        private void btnPuse_Click(object sender, EventArgs e)
        {
            MusicPlayer.Ctlcontrols.pause();
        }


        private void btnStop_Click(object sender, EventArgs e)
        {
            MusicPlayer.Ctlcontrols.stop();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            MusicPlayer.settings.autoStart = false;
            //MusicPlayer.URL = @"C:\Users\Administrator\Desktop\Net\1初級--net基礎\第10天--高級--面向對象多態\2\1、複習~1.avi";
            label1.Image = Image.FromFile("Play.jpg");
        }


        bool flag = true;
        private void btnPlayOrPause_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnPlayOrPause.Text == "播放")
                {
                    if (flag)
                    {
                        MusicPlayer.URL = listPath[lbMusics.SelectedIndex];
                    }
                    MusicPlayer.Ctlcontrols.play();
                    btnPlayOrPause.Text = "暫停";
                }
                else if (btnPlayOrPause.Text == "暫停")
                {
                    MusicPlayer.Ctlcontrols.pause();
                    btnPlayOrPause.Text = "播放";
                    flag = false;
                }
            }
            catch { }
        }


        private void btnStopTwo_Click(object sender, EventArgs e)
        {
            MusicPlayer.Ctlcontrols.stop();
        }


        /// <summary>
        /// 選擇音樂文件,並加載到listBox中
        /// </summary>
        List<string> listPath = new List<string>();
        private void btnOpen_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "音樂文件|*.mp3|WAV文件|*.WAV|所有文件|*.*";
                //ofd.InitialDirectory = @"C:\Users\Administrator\Desktop\Music";
                ofd.InitialDirectory = "Music";
                ofd.Multiselect = true;
                ofd.ShowDialog();


                //將選中的文件保存到數組中
                string[] strPath = ofd.FileNames;
                //循環將選中的音樂文件顯示在ListBox中
                for (int i = 0; i < strPath.Length; i++)
                {
                    lbMusics.Items.Add(strPath[i].Substring(strPath[i].LastIndexOf('\\') + 1));
                    listPath.Add(strPath[i]);
                }
                //加載完畢後讓listBox中默認選擇第一行數據
                lbMusics.SelectedIndex = 0;
            }
            catch { }
        }


        /// <summary>
        /// 雙擊listBox中的音樂列表播放音樂
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbMusics_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (lbMusics.Items.Count == 0)
                {
                    MessageBox.Show("請選擇音樂文件...");
                    return;
                }
                if (lbMusics.SelectedIndex != -1)
                {
                    string strSelectPath = listPath[lbMusics.SelectedIndex];
                    MusicPlayer.URL = strSelectPath;
                    MusicPlayer.Ctlcontrols.play();
                    btnPlayOrPause.Text = "暫停";
                    ExistsLrc(listPath[lbMusics.SelectedIndex]);
                }
            }
            catch { }
        }


        /// <summary>
        /// 單擊播放上一曲
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPre_Click(object sender, EventArgs e)
        {
            try
            {
                int index = lbMusics.SelectedIndex;
                if (index == 0)
                {
                    index = listPath.Count;
                }
                index--;
                lbMusics.SelectedIndex = index;
                MusicPlayer.URL = listPath[index];
                MusicPlayer.Ctlcontrols.play();
                btnPlayOrPause.Text = "暫停";
            }
            catch { }
        }


        /// <summary>
        /// 單擊播放下一曲
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                int index = lbMusics.SelectedIndex;
                index++;
                if (index == listPath.Count)
                {
                    index = 0;
                }
                lbMusics.SelectedIndex = index;
                MusicPlayer.URL = listPath[index];
                MusicPlayer.Ctlcontrols.play();
                btnPlayOrPause.Text = "暫停";
            }
            catch { }
        }


        /// <summary>
        /// 右鍵刪除歌曲列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void contextMenuStrip1_Click(object sender, EventArgs e)
        {
            try
            {
                if (lbMusics.SelectedItems.Count != 0)
                {
                    for (int i = 0; i < lbMusics.SelectedItems.Count; i++)
                    {
                        listPath.RemoveAt(lbMusics.SelectedIndex);
                        lbMusics.Items.RemoveAt(lbMusics.SelectedIndex);
                    }
                }
            }
            catch { }
        }


        /// <summary>
        /// 切換靜音與放音
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label1_Click(object sender, EventArgs e)
        {
            try
            {
                if (label1.Tag.ToString() == "1")
                {
                    MusicPlayer.settings.mute = true;
                    label1.Image = Image.FromFile("Stop.jpg");
                    label1.Tag = "2";
                }
                else if (label1.Tag.ToString() == "2")
                {
                    MusicPlayer.settings.mute = false;
                    label1.Image = Image.FromFile("Play.jpg");
                    label1.Tag = "1";
                }
            }
            catch { }
        }


        /// <summary>
        /// 自動播放下一曲
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (MusicPlayer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                if (double.Parse(MusicPlayer.currentMedia.duration.ToString()) <= (double.Parse(MusicPlayer.Ctlcontrols.currentPosition.ToString()) + 1))
                {
                    try
                    {
                        int index = lbMusics.SelectedIndex;
                        index++;
                        if (index == listPath.Count)
                        {
                            index = 0;
                        }
                        lbMusics.SelectedIndex = index;
                        MusicPlayer.URL = listPath[index];
                        MusicPlayer.Ctlcontrols.play();
                        btnPlayOrPause.Text = "暫停";
                    }
                    catch { }
                }
            }
        }


        void ExistsLrc(string lrcPath)
        {
            try
            {
                lrcPath += ".lrc";
                if (File.Exists(lrcPath))
                {
                    string[] lrcText = File.ReadAllLines(lrcPath, Encoding.Default);
                    //格式化歌詞
                    FormateLrc(lrcText);


                }
                else
                {
                    lblLrc.Text = "=================未找到歌詞=================";
                }
            }
            catch { }
        }


        List<double> listTime = new List<double>();
        List<string> listLrc = new List<string>();


        void FormateLrc(string[] lrcText)
        {
            try
            {
                for (int i = 0; i < lrcText.Length; i++)
                {
                    string[] lrcTemp = lrcText[i].Split(new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries);
                    string[] newLrcTemp = lrcTemp[0].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    double time = (double.Parse(newLrcTemp[0]) * 60) + double.Parse(newLrcTemp[1]);
                    //將獲得的時間保存到集合中
                    listTime.Add(time);
                    //將時間對應的歌詞保存到集合中
                    listLrc.Add(lrcTemp[1]);
                }
            }
            catch { }
        }


        private void timer2_Tick(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < listTime.Count; i++)
                {
                    if (MusicPlayer.Ctlcontrols.currentPosition >= listTime[i] && MusicPlayer.Ctlcontrols.currentPosition < listTime[i + 1])
                    {
                        lblLrc.Text = listLrc[i];
                    }
                }
            }
            catch { }
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章