码迷,mamicode.com
首页 > Windows程序 > 详细

Winform Windows Media Player 简易播放器

时间:2014-07-31 20:47:37      阅读:419      评论:0      收藏:0      [点我收藏+]

标签:winform   windows media player   播放器   

新手上路,高手勿进!

窗体设计:

bubuko.com,布布扣

实现效果:

bubuko.com,布布扣

实现代码:

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.Data.SqlClient;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections;
using System.Media;


namespace Music {
    public partial class LenMusic : Form {
        public LenMusic() {
            InitializeComponent();
        }


        #region
        string strMusic = "";//音乐路径
        string strImage = "";//图片路径
        string strGeci = "";//歌词路径

        ArrayList musicNameList = new ArrayList();//所有文件的名称     
        ArrayList imageNameList = new ArrayList();//所有图片的名称
        ArrayList geciNameList = new ArrayList();//所有歌词路径

        int allcount = 0;//所有条数
        int nowcount = 0;//当前歌曲的索引
        string abcMusic = "";//记录当前播放的歌曲名称,用于单曲循环播放

        bool MaxOrNo = false;//判断一个窗体是否是最大化

        int txtn = 0;//txt文件的行数
        #endregion

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);

        private string strFilePath = Application.StartupPath + "\\Path.ini";//获取INI文件路径
        private string strSec = "";//INI文件名     

        /// <summary>
        /// 自定义读取INI文件中的内容方法
        /// </summary>
        /// <param name="Section">键</param>
        /// <param name="key">值</param>
        /// <returns></returns>
        private string ContentValue(string Section, string key) {
            StringBuilder temp = new StringBuilder(1024);
            GetPrivateProfileString(Section, key, "", temp, 1024, strFilePath);
            return temp.ToString();
        }

        #region Load事件
        private void LenMusic_Load(object sender, EventArgs e) {
            //设置层显示
            grbSet.Visible = false;
            pnlMusic.Visible = true;
            //歌词路径赋值
            strGeci = Application.StartupPath + "\\txtGeCi";

            #region 获取歌曲名称,图片名称,歌词名称
            if (File.Exists(strFilePath))//读取时先要判读INI文件是否存在
            {
                strSec = Path.GetFileNameWithoutExtension(strFilePath);
                strMusic = ContentValue(strSec, "musicPath");
                strImage = ContentValue(strSec, "imagePath");
                //设置路径的默认值
                string XiangDuiPath = System.AppDomain.CurrentDomain.BaseDirectory;
                if (strMusic.Equals("")) {
                    strMusic = XiangDuiPath + @"MP3";
                }
                if (strImage.Equals("")) {
                    strImage = XiangDuiPath + @"images";
                }
            } else {
                MessageBox.Show("INI文件不存在");
                return;
            }

            // 取得指定路径下所有符合条件的文件
            string[] strFiles = Directory.GetFiles(strMusic, "*.*");
            // 取得指定路径下所有目录
            //string[] strDirs = Directory.GetDirectories(strMusic);           


            //foreach (string lujing in strDirs) {
            //    musicList.Add(lujing);
            //}
            foreach (string name in strFiles) {
                musicNameList.Add(GetFileName(name));
            }

            //获取指定目录下所有图片的名称
            string[] strFilesImage = Directory.GetFiles(strImage, "*.*");
            foreach (string name in strFilesImage) {
                imageNameList.Add(GetFileName(name));
            }

            //获取指定目录下所有歌词的名称
            string[] strFilesGeci = Directory.GetFiles(strGeci, "*.*");
            foreach (string name in strFilesGeci) {
                geciNameList.Add(GetFileName(name));
            }
            #endregion

            #region ListView设置
            listViewAll.GridLines = true;
            listViewAll.FullRowSelect = true;
            listViewAll.View = View.Details;//使用哪一个视图
            listViewAll.Scrollable = false;
            listViewAll.MultiSelect = false;
            listViewAll.HeaderStyle = ColumnHeaderStyle.Clickable;

            listViewAll.Columns.Add("序号", 40, HorizontalAlignment.Right);
            listViewAll.Columns.Add("歌曲", 173, HorizontalAlignment.Left);

            //设置行高
            ImageList list = new ImageList();
            list.ImageSize = new Size(1, 30);
            listViewAll.SmallImageList = list;

            //加载ListView数据
            for (int i = 0; i < musicNameList.Count; i++) {
                ListViewItem lvi = new ListViewItem();
                lvi.SubItems.Clear();
                lvi.Text = (i + 1).ToString();//序号列
                lvi.SubItems.Add(musicNameList[i].ToString());//歌曲名称
                listViewAll.Items.Add(lvi);
            }
            #endregion


            #region 歌曲默认为第一首
            try {
                //歌曲默认为第一首
                labName.Text = musicNameList[0].ToString();
            } catch {
                labName.Text = "歌曲名称";
            }
            #endregion


            //歌曲总数
            allcount = musicNameList.Count;


            //设置模式
            this.axWindowsMediaPlayer1.uiMode = "None";


            #region 声音加载

            this.tbVoice.Minimum = 0;//声音最小值
            this.tbVoice.Maximum = 100;//声音最大值
            //设置值
            this.tbVoice.Value = axWindowsMediaPlayer1.settings.volume;
            #endregion


            #region 透明 设置


            #region 设置pnlFontScoll
            this.pnlMusic.SendToBack();//将背景图片放到最下面
            this.pnlFontScoll.BackColor = Color.Transparent;//将Panel设为透明
            this.pnlFontScoll.Parent = this.pnlMusic;//将panel父控件设为背景图片控件
            this.pnlFontScoll.BringToFront();//将panel放在前面            
            #endregion


            #region 歌词透明,歌词字体设置
            //透明Label
            this.pbPicture.SendToBack();
            this.labGeci.BackColor = Color.Transparent;
            this.labGeci.Parent = this.pbPicture;
            this.labGeci.BringToFront();


            //设置字体和颜色
            this.labGeci.ForeColor = Color.Yellow;
            this.labGeci.Font = new Font("华文行楷", 14);
            #endregion


            #region 透明层 设置picturebox
            this.pbPicture.SendToBack();//将背景图片放到最下面
            this.pnlTouMing.BackColor = Color.Transparent;//将Panel设为透明
            this.pnlTouMing.Parent = this.pbPicture;//将panel父控件设为背景图片控件
            this.pnlTouMing.BringToFront();//将panel放在前面           
            #endregion


            //listViewAll 不支持背景色透明

            #endregion


        }
        #endregion


        #region 获取文件名称
        public static string GetFileName(String path) {
            if (path.Contains("\\")) {
                string[] arr = path.Split(‘\\‘);
                return arr[arr.Length - 1];
            } else {
                string[] arr = path.Split(‘/‘);
                return arr[arr.Length - 1];
            }
        }
        #endregion


        #region 系统设置
        private void pbSet_Click(object sender, EventArgs e) {
            pnlMusic.Visible = false;
            grbSet.Visible = true;


            //从INI文件中读取数据
            if (File.Exists(strFilePath))//读取时先要判读INI文件是否存在
            {
                strSec = Path.GetFileNameWithoutExtension(strFilePath);
                txtMusicPath.Text = ContentValue(strSec, "musicPath");
                txtImagePath.Text = ContentValue(strSec, "imagePath");
            } else {
                MessageBox.Show("INI文件不存在");
                return;
            }


        }


        //打开音乐路径
        private void btnMusicPath_Click(object sender, EventArgs e) {
            OpenFileDialog open = new OpenFileDialog();
            open.Title = "请选择音乐文件!";
            if (open.ShowDialog() == DialogResult.OK) {
                txtMusicPath2.Text = open.FileName;
            }

        }

        private void btnMusicPath_Click_1(object sender, EventArgs e) {
            fbdMusic.Description = "请选择音乐文件!";
            if (fbdMusic.ShowDialog() == DialogResult.OK) {
                txtMusicPath.Text = fbdMusic.SelectedPath;
            }
        }

        //打开音乐图片路径
        private void btnImagePath_Click(object sender, EventArgs e) {
            //fbdImages.t
            fbdImages.Description = "选择的图片将在播放音乐时作为背景图片!";
            if (fbdImages.ShowDialog() == DialogResult.OK) {
                txtImagePath.Text = fbdImages.SelectedPath;
            }
        }

        //保存,成功则自动关闭
        private void btnSave_Click(object sender, EventArgs e) {
            if (txtMusicPath.Text.Trim().Equals("")) {
                MessageBox.Show("音乐路径不能为空!");
                return;
            }
            if (txtImagePath.Text.Trim().Equals("")) {
                MessageBox.Show("图片路径不能为空!");
                return;
            }

            //StreamWriter sw = new StreamWriter(System.AppDomain.CurrentDomain + "\\Path.ini",true);
            //sw.WriteLine(txtMusicPath.Text);
            //sw.WriteLine(txtImagePath.Text);
            //sw.Flush();
            //sw.Close();
            try {
                strSec = Path.GetFileNameWithoutExtension(strFilePath);
                WritePrivateProfileString(strSec, "musicPath", txtMusicPath.Text.Trim(), strFilePath);
                WritePrivateProfileString(strSec, "imagePath", txtImagePath.Text.Trim(), strFilePath);
                MessageBox.Show("保存成功!");
                grbSet.Visible = false;
                pnlMusic.Visible = true;
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return;
            }
        }


        //返回主界面
        private void btnReturn_Click(object sender, EventArgs e) {
            grbSet.Visible = false;
            pnlMusic.Visible = true;
        }
        #endregion


        #region  从列表中读取选中第一列的值,最上面显示歌曲名称
        private void listViewAll_SelectedIndexChanged(object sender, EventArgs e) {
            if (listViewAll.SelectedIndices != null && listViewAll.SelectedIndices.Count > 0) {
                ListView.SelectedIndexCollection c = listViewAll.SelectedIndices;
                string suoyin = listViewAll.Items[c[0]].Text;
                nowcount = Convert.ToInt32(suoyin) - 1;
                //改变歌曲名称
                labName.Text = musicNameList[Convert.ToInt32(suoyin) - 1].ToString();
                try {
                    //显示图片
                    pbPicture.Image = new Bitmap(strImage + "\\" + imageNameList[Convert.ToInt32(suoyin) - 1].ToString());
                    ReadTXT(labGeci);
                } catch (Exception) {


                }
            }
        }
        #endregion


        #region Listview 双击事件,播放音乐
        private void listViewAll_DoubleClick(object sender, EventArgs e) {
            axWindowsMediaPlayer1.URL = strMusic + "\\" + labName.Text;
            axWindowsMediaPlayer1.Ctlcontrols.play();


            //歌词
            ReadTXT(labGeci);
        }
        #endregion


        #region 播放/暂停
        private void btnStop_Click(object sender, EventArgs e) {
            try {
                #region SoundPlayer 只可以播放波形文件(.wav格式)
                //System.Media.SoundPlayer sp=new System.Media.SoundPlayer();
                ////SoundPlayer sp = new SoundPlayer();
                //sp.SoundLocation = strMusic +"\\"+ labName.Text;               
                //sp.Load();
                //sp.Play();
                #endregion


                if (btnStart.Text == ">") {
                    if (radOneYes.Checked == true) {
                        axWindowsMediaPlayer1.URL = strMusic + "\\" + abcMusic;
                    } else {
                        //播放  
                        axWindowsMediaPlayer1.URL = strMusic + "\\" + labName.Text;
                    }
                   
                    //记录播放位置
                    if (tbMusic.Value != 0) {
                        axWindowsMediaPlayer1.Ctlcontrols.currentPosition = tbMusic.Value;
                    }
                    axWindowsMediaPlayer1.Ctlcontrols.play();
                    //歌词
                    ReadTXT(labGeci);
                    timerGeci.Enabled = true;


                    timer1.Enabled = true;
                    btnStart.Text = "||";


                    //透明层显示
                    pnlTouMing.Visible = false;


                    #region 一些属性
                    //string Duration = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("Duration");//持续时间(秒)
                    //string Title = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");//媒体标题
                    //string Author = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("Author");//艺术家
                    //string Copyright = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("Copyright");//版权信息
                    //string Description = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("Description");//媒体内容描述
                    //string FileSize = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("FileSize");//文件大小
                    //string FileType = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("FileType");//文件类型
                    //string sourceURL = this.axWindowsMediaPlayer1.currentMedia.getItemInfo("sourceURL");//原始地址
                    //MessageBox.Show("持续时间:" + Duration + "||" + "媒体标题:" + Title + "||" + "艺术家:" + Author + "||" + "版权信息:" + Copyright + "||" + "媒体内容描述:" + Description + "||" + "文件大小:" + FileSize + "||" + "文件类型:" + FileType + "||" + "原始地址:" + sourceURL);
                    #endregion
                    return;
                }
                if (btnStart.Text == "||") {
                    //暂停        
                    axWindowsMediaPlayer1.Ctlcontrols.pause();
                    timerGeci.Enabled = false;
                    timer1.Enabled = false;
                    btnStart.Text = ">";
                    //透明层显示
                    pnlTouMing.Visible = true;
                    return;
                }

            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return;
            }
        }
        #endregion

        #region 上一曲
        private void btnUp_Click(object sender, EventArgs e) {
            for (int i = 0; i < allcount; i++) {
                if (musicNameList[i].ToString() == labName.Text) {
                    nowcount = i;
                }
            }
            //当前行所有大于0,则上一曲的索引最新为0
            if (nowcount > 0) {
                labName.Text = musicNameList[nowcount - 1].ToString();

                //显示图片
                pbPicture.Image = new Bitmap(strImage + "\\" + imageNameList[nowcount - 1].ToString());
                //显示歌词
                ReadTXT(labGeci);

                axWindowsMediaPlayer1.URL = strMusic + "\\" + labName.Text;
                axWindowsMediaPlayer1.Ctlcontrols.play();
                //歌词
                ReadTXT(labGeci);

            } else {
                return;
            }

        }
        #endregion

        #region 下一曲
        private void btnNext_Click(object sender, EventArgs e) {
            for (int i = 0; i < allcount; i++) {
                if (musicNameList[i].ToString() == labName.Text) {
                    nowcount = i;
                }
            }
            if (nowcount < allcount - 1) {
                labName.Text = musicNameList[nowcount + 1].ToString();
                //显示图片
                pbPicture.Image = new Bitmap(strImage + "\\" + imageNameList[nowcount + 1].ToString());
                //显示歌词
                ReadTXT(labGeci);
                axWindowsMediaPlayer1.URL = strMusic + "\\" + labName.Text;
                axWindowsMediaPlayer1.Ctlcontrols.play();
                //歌词
                ReadTXT(labGeci);

            } else {
                return;
            }
        }
        #endregion

        #region 停止
        private void btnStop_Click_1(object sender, EventArgs e) {           
            axWindowsMediaPlayer1.Ctlcontrols.stop();
           // axWindowsMediaPlayer1.URL = strMusic + "\\" + labName.Text;
            btnStart.Text = ">";
            tbMusic.Value = 0;
            labDateNow.Text = "00:00";            
        }
        #endregion

        #region 播放进度条
        //进度条
        private void timer1_Tick(object sender, EventArgs e) {
            //axWindowsMediaPlayer1.URL = strMusic + "\\" + labName.Text;
            if (axWindowsMediaPlayer1.currentMedia != null) {
                tbMusic.Maximum = Convert.ToInt32(axWindowsMediaPlayer1.currentMedia.duration);
                tbMusic.Minimum = 0;
                tbMusic.Value = Convert.ToInt32(axWindowsMediaPlayer1.Ctlcontrols.currentPosition);
                //字符串类型的当前时间
                labDateNow.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPositionString.ToString();
                //字符串类型的时长
                labDateAll.Text = axWindowsMediaPlayer1.currentMedia.durationString.ToString();
            }

            //是否循环播放
            if (chkXunHuan.Checked == true) {//判断是单曲循环还是多曲循环
                if (radOneYes.Checked == true) {//单曲循环
                    //如果进度条到达最大位置,则设置为最小值
                if (tbMusic.Value == 0) {
                    btnStart.Text = ">";
                    btnStart.PerformClick();
                    }
                } else { //多曲循环
                    //如果进度条到达最大位置,并且这一曲不是最好一曲就播放下一曲
                    if (tbMusic.Value == tbMusic.Maximum && axWindowsMediaPlayer1.currentMedia.getItemInfo("Title") != musicNameList[musicNameList.Count - 1].ToString()) {
                        btnNext.PerformClick();
                    }
                }
            } 

        }
        #endregion

        #region 改变音量的大小
        private void tbVoice_Scroll(object sender, EventArgs e) {
            this.axWindowsMediaPlayer1.settings.volume = this.tbVoice.Value;
        }
        #endregion


        #region 快进、快退
        //快进,卡
        private void btnUpup_Click(object sender, EventArgs e) {
            axWindowsMediaPlayer1.Ctlcontrols.fastReverse();
        }
        //快退
        private void btnNextnext_Click(object sender, EventArgs e) {
            axWindowsMediaPlayer1.Ctlcontrols.fastForward();
        }
        #endregion

        #region 改变播放进度
        private void tbMusic_Scroll(object sender, EventArgs e) {
            axWindowsMediaPlayer1.Ctlcontrols.currentPosition = tbMusic.Value;
            //this.axWindowsMediaPlayer1.currentMedia.setItemInfo("volume", 60);
        }
        #endregion

        #region 点击声音按钮,音量是否显示
        private void pbVoice_Click(object sender, EventArgs e) {
            if (tbVoice.Visible == false) {
                tbVoice.Visible = true;
                return;
            }
            if (tbVoice.Visible == true) {
                tbVoice.Visible = false;
                return;
            }
        }

        #endregion

        #region 歌曲名称滚动字幕
        private void timerFont_Tick(object sender, EventArgs e) {           
            labName.Left = labName.Left - 3;
            if (labName.Right < 0) {
                labName.Left = pnlFontScoll.Width;
            } 
        }
        #endregion

        #region 单曲循环、多曲循环   显示
        private void chkXunHuan_CheckedChanged(object sender, EventArgs e) {
            if (chkXunHuan.Checked == true) {
                pnlXunHuan.Visible = true;
                return;
            } else {
                pnlXunHuan.Visible = false;
                return;
            }
        }

        private void chkXunHuan_MouseMove(object sender, MouseEventArgs e) {
            if (chkXunHuan.Checked == true) {
                pnlXunHuan.Visible = true;
                return;
            } 
        }

        private void pnlXunHuan_MouseMove(object sender, MouseEventArgs e) {
            if (chkXunHuan.Checked == true) {
                int a = MouseInPanel(pnlXunHuan);
                if (a == 1) {
                    pnlXunHuan.Visible = true;
                } else {
                    pnlXunHuan.Visible = false;
                }
            }
        }

        #region 判断鼠标位置是否在一个层上
        private int MouseInPanel(Panel panel) {
            //    当前的屏幕除任务栏外的工作域大小
            //    this.Width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
            //    this.Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;


            //    当前的屏幕包括任务栏的工作域大小
            //    this.Width=System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
            //    this.Height=System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;

            // Cursor.Position.X;//鼠标的坐标,相对于屏幕
            // Cursor.Position.Y;

            int kuan = pnlMusic.Width;
            int gao = pnlMusic.Height;

            int x = Cursor.Position.X - (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width-kuan) / 2;
            int y = Cursor.Position.Y - (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height-gao) / 2;

            int px = panel.Location.X;//panel位置
            int py = panel.Location.Y;
            int pw = panel.Width;//panel宽和高
            int ph = panel.Height;           
            if (x > px && y > py && x < px + pw && y < py + ph) {
                return 1;
            } else {
                return 0;
            }
        }
        #endregion


        //获取窗体鼠标的位置,但是不执行
        private void LenMusic_MouseMove(object sender, MouseEventArgs e) {
            //mx = e.X;
            //my = e.Y;
           //label2.Text = e.X + "," + e.Y;
            //mx = MousePosition.X - this.Width;
            //my = MousePosition.Y - this.Height;
        }

        #endregion


        #region 单曲被选中,把这首歌的名称赋值给一个变量
        private void radOneYes_CheckedChanged(object sender, EventArgs e) {
            if (radOneYes.Checked == true) {
                abcMusic = labName.Text;
            }
        }
        #endregion
        //待解决问题,选中单曲循环时,会自动播放


        #region 点击透明层上的按钮  播放
        private void pbStart_Click(object sender, EventArgs e) {
            if (btnStart.Text == ">") {
                if (radOneYes.Checked == true) {
                    axWindowsMediaPlayer1.URL = strMusic + "\\" + abcMusic;
                } else {
                    //播放  
                    axWindowsMediaPlayer1.URL = strMusic + "\\" + labName.Text;
                }

                //记录播放位置
                if (tbMusic.Value != 0) {
                    axWindowsMediaPlayer1.Ctlcontrols.currentPosition = tbMusic.Value;
                }
                axWindowsMediaPlayer1.Ctlcontrols.play();

                timer1.Enabled = true;
                btnStart.Text = "||";

                //透明层显示
                pnlTouMing.Visible = false;
                return;
            }


            if (btnStart.Text == "||") {
                //暂停        
                axWindowsMediaPlayer1.Ctlcontrols.pause();
                timer1.Enabled = false;
                btnStart.Text = ">";
                //透明层显示
                pnlTouMing.Visible = false;
            }           
            return;
        }
        #endregion


        #region 点击显示、关闭透明层
        private void pbPicture_Click(object sender, EventArgs e) {
            if (pnlTouMing.Visible == true) {
                pnlTouMing.Visible = false;
            } else {
                if (btnStart.Text == ">") {
                    pbStart.Image = new Bitmap(Application.StartupPath + "\\image\\start.png");
                } else {
                    pbStart.Image = new Bitmap(Application.StartupPath + "\\image\\start2.png");
                }
                pnlTouMing.Visible = true;
            }
        }
        #endregion

        #region 双击图片铺满屏幕、双击铺满窗体
        private void pbPicture_DoubleClick(object sender, EventArgs e) {

            #region 双击图片铺满窗体(设置窗体层的背景图片),效果不好      
            //pnlMusic.BringToFront();
            //pnlMusic.BackgroundImage = System.Drawing.Image.FromFile(strImage+"\\" + imageNameList[nowcount].ToString());
            ////设置背景图片自动适应
            //pnlMusic.BackgroundImageLayout = ImageLayout.Stretch;        


            #endregion


            #region 双击,图片全屏


            if (MaxOrNo == false) {//不是最大化  
                //把窗体设置为全屏
                this.WindowState = FormWindowState.Maximized;
                pnlMusic.Dock = System.Windows.Forms.DockStyle.Fill; //设置层的格式为充满
                pbPicture.Parent = this.pnlMusic; //设置图片的父容器为层
                pbPicture.Location = new System.Drawing.Point(0, 0);//把pbPicture的坐标设置为(0,0)
                pbPicture.BringToFront(); //把pbPicture设置为顶层
                //设置图片的宽和高
                pbPicture.Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
                pbPicture.Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;               
                //设置播放按钮的位置
                pnlTouMing.Location = new System.Drawing.Point(pbPicture.Width / 2, pbPicture.Height / 2 - 300);
                MaxOrNo = true;//改变状态
                
            } else { //是最大化
                //this.WindowState = FormWindowState.Normal;
                //pbPicture = pb;
                //pnlMusic.SendToBack();
                //pnlMusic.Dock = System.Windows.Forms.DockStyle.None;
                MaxOrNo = false;//改变状态
            }
            #endregion
        }
        #endregion

        #region 是否显示歌词
        private void chkGeCi_CheckedChanged(object sender, EventArgs e) {
            if (chkGeCi.Checked == true) {
                labGeci.Visible = true;
                timerGeci.Enabled = true;
            } else {
                labGeci.Visible = false;
                timerGeci.Enabled = false;
            }
        }
        #endregion
        
        #region 读取TXT 文件
        private void ReadTXT(Label lab) {
            txtn = 0;//每次读取txt时,清空上一个TXT的行数
            string strTxtAll = "";
            string abc = strGeci + "\\" + geciNameList[nowcount].ToString();
            FileStream fs = new FileStream(abc, FileMode.Open, FileAccess.Read, FileShare.None);
            StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("GB2312"));
            string line = sr.ReadLine();
            strTxtAll = line + "\r\n";
            for (int ac = 0; line != null; ac++) {
                line = sr.ReadLine();
                txtn++;
                strTxtAll += line + "\r\n";
            }
            lab.Text = strTxtAll;
            lab.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            sr.Close();
            fs.Close();
        }
        #endregion

        #region 歌词滚动
        private void timerGeci_Tick(object sender, EventArgs e) {
            //获取步长
            //int n = 0;//步长
            //if (axWindowsMediaPlayer1.currentMedia != null) {
            //    double nAll = axWindowsMediaPlayer1.currentMedia.duration; //获取当前歌曲的时长
            //    ReadTXT(labGeci);//给Label赋值
            //    //计算时是以毫秒为单位的 Interval=1000;
            //    n = Convert.ToInt32(labGeci.Height / nAll);
            //    //MessageBox.Show(labGeci.Height + "," + nAll.ToString());
            //    return;
            //}
            //滚动
            labGeci.Top = labGeci.Top - 3;
            if (labGeci.Bottom < 0) {
                labGeci.Top = pbPicture.Width;
            }
        }
        #endregion

    }

}


Winform Windows Media Player 简易播放器,布布扣,bubuko.com

Winform Windows Media Player 简易播放器

标签:winform   windows media player   播放器   

原文地址:http://blog.csdn.net/u013816709/article/details/38322629

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!