在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → DMPlayer播放器 完整源代码

DMPlayer播放器 完整源代码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:23.40M
  • 下载次数:65
  • 浏览次数:569
  • 发布时间:2018-10-11
  • 实例类别:C#语言基础
  • 发 布 人:songjian070
  • 文件格式:.7z
  • 所需积分:2
 相关标签: 播放器 源代码 代码

实例介绍

【实例简介】

【实例截图】

from clipboard


from clipboard

【核心代码】

 

#region 绘制主界面
        SolidBrush sb = new SolidBrush(Color.FromArgb(32, 191, 99));
        protected override void OnPaint(PaintEventArgs e)
        {
            //设置高质量插值法
            e.Graphics.InterpolationMode = InterpolationMode.Bilinear;
            //设置高质量,低速度呈现平滑程度
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            e.Graphics.FillRectangle(sb, -1, -1, Width 30, Height 30);

            if (PlayerStae!= PlayState.Stop)
            {
                //绘制播放标题
                if (CurrentFile != null && CurrentFile.FileName.Trim() != "")
                {
                    string Temp = CurrentFile.FileName;
                    int wit = Convert.ToInt32(e.Graphics.MeasureString(Temp, TitleFont).Width); ;
                    while (wit > Width - 250)
                    {
                        Temp = Temp.Substring(0, Temp.Length - 2);
                        wit = Convert.ToInt32(e.Graphics.MeasureString(Temp, TitleFont).Width);
                    }
                    e.Graphics.DrawString(Temp, TitleFont, new SolidBrush(Color.White), new Point(Width / 2 - wit / 2, 12));
                }
            }
        }
        #endregion

        #region 界面操作
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            if (op.ShowDialog() == DialogResult.OK)
            {
                PlayFile p = new PlayFile();
                p.FileUrl = op.FileName;
                p.FileName = op.SafeFileName;
                Play(p);
            }
        }
        #endregion

        #region 界面效果
        private void btn_MouseEnter(object sender, EventArgs e)
        {
            DMSkin.Controls.DMLabel dm = (DMSkin.Controls.DMLabel)sender;
            dm.DM_Color = Color.FromArgb(192, 255, 192);
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            DMSkin.Controls.DMLabel dm = (DMSkin.Controls.DMLabel)sender;
            dm.DM_Color = Color.White;
        }

        private void Listbtn_MouseEnter(object sender, EventArgs e)
        {
            DMSkin.Controls.DMLabel dm = (DMSkin.Controls.DMLabel)sender;
            dm.DM_Color = Color.SeaGreen;
        }

        private void Listbtn_MouseLeave(object sender, EventArgs e)
        {
            DMSkin.Controls.DMLabel dm = (DMSkin.Controls.DMLabel)sender;
            dm.DM_Color = Color.MediumSeaGreen;
        }
        #endregion

        #region 播放
        bool IsMouseDown = false;//鼠标按下
        int ToPostion = 0;//目标进度
        int CurrentLength = 0;
        double CurrentPosition = 0.0;
        PlayFile CurrentFile = new PlayFile();
        public List<PlayFile> PlayLists = new List<PlayFile>();
        public void Play(PlayFile file)
        {
            Player.SetConfig(2201,API.SavePath file.FileName.Trim() ".dm");
            CurrentFile = file;
            Player.Close();
            InitPlay();
            Player.Open(file.FileUrl);
            btnPlay.DM_Key = DMSkin.Controls.DMLabelKey.暂停;
            PlayerStae = PlayState.Play;
            this.Invalidate();
        }
        public void TurnPlayPage()
        {
            PlayListView.SelectedIndex = 0;
        }
        Font TitleFont = new Font("微软雅黑", 12, FontStyle.Bold);

        public void Play()
        {
            Player.Play();
            PlayerStae = PlayState.Play;
            btnPlay.DM_Key = DMSkin.Controls.DMLabelKey.暂停;
        }
        public void Stop()
        {
            InitPlay();
            CurrentFile.FileName = "";
            labTime.Text = "00:00:00/00:00:00";
            Player.Close();
            PlayerStae = PlayState.Stop;
            this.Invalidate();
        }
        /// <summary>
        /// 往后
        /// </summary>
        private void PlayBack()
        {
            int index = Convert.ToInt32(CurrentPosition);
            int toindex = index - 4000;
            Player.SetPosition(toindex);
            CurrentPosition = Convert.ToDouble(Player.GetPosition());
        }
        /// <summary>
        /// 往前
        /// </summary>
        private void PlayGo()
        {
            int index = Convert.ToInt32(CurrentPosition);
            int toindex = index 4000;
            Player.SetPosition(toindex);
            CurrentPosition = Convert.ToDouble(Player.GetPosition());
        }
        /// <summary>
        /// 上一首
        /// </summary>
        private void PlayPrevious()
        {
            
        }
        /// <summary>
        /// 下一首
        /// </summary>
        private void PlayNext()
        {

        }
        public int GetBufferProgress()
        {
            //int IDS =;
            return Player.GetBufferProgress();
        }
        public int GetVolume() 
        {
            return Player.GetVolume();
        }
        public void SetVolume(int num)
        {
            Player.SetVolume(num);
            this.toolTip.Show(Convert.ToInt32(dmVolumeProgress.DM_Value).ToString(), dmVolumeProgress);
        }
        public void Pause()
        {
            Player.Pause();
            PlayerStae = PlayState.Pause;
            btnPlay.DM_Key = DMSkin.Controls.DMLabelKey.播放;
        }
        public void InitPlay()
        {
            btnPlay.DM_Key = DMSkin.Controls.DMLabelKey.播放;
            CurrentLength = 0;
            CurrentPosition = 0.0;
            ToPostion = 0;
            dmProgressBar.DM_Value = 0;
        }
        /// <summary>
        /// 时间转换,将秒转换为对应的   mm:ss
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string ConvertTime(int time)
        {
            string musicTime = "00:00";
            if (time > 0)
            {
                int minute = time / 60;
                int seconds = time % 60;
                musicTime = (minute < 10 ? "0" minute.ToString() : minute.ToString()) ":" (seconds < 10 ? "0" seconds.ToString() : seconds.ToString());
            }
            return musicTime;
        }
        public void ShowTime()
        {
            string str1 = ConvertTime(Player.GetPosition() / 1000);
            string str2 = ConvertTime(Player.GetDuration() / 1000);
            if (str1.IndexOf(".") >= 0)
            {
                str1 = str1.Substring(0, str1.LastIndexOf("."));
            }
            if (str2.IndexOf(".") >= 0)
            {
                str2 = str2.Substring(0, str2.LastIndexOf("."));
            }
            labTime.Text = str1 "/" str2;
        }
        bool Full = false;
        Rectangle Nor = new Rectangle(0, 0, 0, 0);
        //全屏
        public void FullScreen()
        {
            if (Full)
            {
                this.WindowState = FormWindowState.Normal;
                axPlayer.Parent = tabPage1;
                axPlayer.BringToFront();
                axPlayer.Location = Nor.Location;
                axPlayer.Size = Nor.Size;
                Full = false;
            }
            else
            {
                Nor = new Rectangle(axPlayer.Location, axPlayer.Size);
                axPlayer.Parent = this;
                this.WindowState = FormWindowState.Maximized;
                axPlayer.BringToFront();
                axPlayer.Location = new Point(0, 0);
                axPlayer.Size = Size;
                Full = true;
            }
            axPlayer.Focus();
        }
        private void timer_Tick(object sender, EventArgs e)
        {
            if (PlayerStae == PlayState.Play)//播放中
            {
                if (!IsMouseDown)//没有拖动
                {
                    CurrentLength = Player.GetDuration();
                    if (CurrentLength == 0)
                    {
                        return;
                    }
                    CurrentPosition = Convert.ToDouble(Player.GetPosition());
                    
                    dmProgressBar.DM_Value = CurrentPosition / Convert.ToDouble(CurrentLength) * 100.0;
                    dmProgressBar.Invalidate();
                    ShowTime();
                }
            }
        }

        private void dmProgressBar_MouseUp(object sender, MouseEventArgs e)
        {
            //进度条鼠标抬起
            IsMouseDown = false;
            if (ToPostion != 0)
            {
                Player.SetPosition(ToPostion);
            }
        }
        private void dmProgressBar_MouseDown(object sender, MouseEventArgs e)
        {
            //进度条鼠标按下
            if (dmProgressBar.Cursor==Cursors.Hand)
            {
                IsMouseDown = true;
            }
        }

        private void dmProgressBar_MouseMove(object sender, MouseEventArgs e)
        {
            //鼠标拖动
            if (IsMouseDown)//鼠标按下
            {
                ToPostion = Convert.ToInt32(dmProgressBar.DM_Value / 100.0 * Convert.ToDouble(Player.GetDuration()));
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            Stop();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            ///上一首
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            ///播放
            if (PlayerStae == PlayState.Play)
            {
                Pause();
                btnPlay.DM_Key = DMSkin.Controls.DMLabelKey.播放;
            }
            else
            {
                Play();
                btnPlay.DM_Key = DMSkin.Controls.DMLabelKey.暂停;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            //下一首

        }
        private void btnMax_Click(object sender, EventArgs e)
        {
            //最大化
            FullScreen();
        }
        

        
        #endregion

        #region 播放器事件
        private void PlayList_ItemClick(object sender, EventArgs e)
        {
            if (Player != null)
            {
                Item it = (Item)sender;
                PlayFile p = new PlayFile();
                p.FileUrl = it.Url;
                p.FileName = it.Text;
                Play(p);
                TurnPlayPage();
            }
        }


        private void axPlayer_OnMessage(object sender, AxAPlayer3Lib._IPlayerEvents_OnMessageEvent e)
        {

            switch (e.nMessage)
            {

                case DMSkin.Win32.WM_LBUTTONDBLCLK://双击
                    FullScreen();
                    break;
                case DMSkin.Win32.WM_LBUTTONDOWN:
                    //右键菜单
                    break;
                default:
                    break;

            }
        }
        Rectangle LastRect;
        bool Max = false;
        /// <summary>
        /// 双击 最大化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Main_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (!Max)
            {
                LastRect = new Rectangle(Location, Size);
                Width = Screen.PrimaryScreen.Bounds.Width;
                Height = Screen.PrimaryScreen.Bounds.Height;
                Location = new Point(0, 0);
                Max = true;
            }
            else
            {
                Width = LastRect.Width;
                Height = LastRect.Height;
                Location = LastRect.Location;
                Max = false;
            }
            axPlayer.Focus();
        }
        #endregion

        #region 播放列表-操作
        /// <summary>
        /// 新增单曲到播放列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            if (op.ShowDialog() == DialogResult.OK)
            {
                PlayFile p = new PlayFile();
                p.FileUrl = op.FileName;
                p.FileName = op.SafeFileName;
                PlayListAdd(p);
                InitPlayListView();
            }
        }

        /// <summary>
        /// 重绘 歌曲列表
        /// </summary>
        public void InitPlayListView()
        {
            PlayListControl.Items.Clear();
            if (PlayLists != null)
            {
                int indexY = 0;
                for (int i = 0; i < PlayLists.Count; i )
                {
                    Item it = new Item();
                    it.Bounds = new Rectangle(0, indexY, 1920, 50);
                    it.Text = PlayLists[i].FileName;
                    it.Url = PlayLists[i].FileUrl;
                    it.OnLine = PlayLists[i].FileOnLine;
                    it.MouseBackColor = Color.FromArgb(224, 224, 224);
                    it.ForeColor = Color.MediumSeaGreen;
                    PlayListControl.Items.Add(it);
                    indexY = 50;
                }
            }
        }

        /// <summary>
        /// 清空播放列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnListClear_Click(object sender, EventArgs e)
        {
            PlayLists.Clear();
            InitPlayListView();
        }

        //选择文件夹 搜索
        private void btnListFileWork_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fd = new FolderBrowserDialog();
            if (fd.ShowDialog() == DialogResult.OK)
            {
                API.LoopFolder(fd.SelectedPath, (System.IO.FileInfo file) =>
                {
                    PlayFile p = new PlayFile();
                    p.FileUrl = file.FullName;
                    p.FileName = file.Name;
                    PlayListAdd(p);
                });
                InitPlayListView();
            }
        }

        public void PlayListAdd(PlayFile p)
        {
            if (PlayLists == null)
            {
                PlayLists = new List<PlayFile>();
            }
            for (int i = 0; i < API.CanPlayFile.Count; i )
            {
                if (p.FileName.ToLower().IndexOf(API.CanPlayFile[i]) >= 0)
                {
                    PlayLists.Add(p);
                }
            }
        }

        private void btnListFileWeb_Click(object sender, EventArgs e)
        {
            Open op = new Open(this);
            op.Show();
        }

        /// <summary>
        /// 切换屏幕比
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dmComboBox_Scrren_DM_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dmComboBox_Scrren.DM_SelectItem.Text == "铺满窗口")
            {
                Player.SetConfig(207, "Set");
            }
            else
            {
                Player.SetConfig(204, dmComboBox_Scrren.DM_SelectItem.Text);
            }
            TurnPlayPage();
        }

        private void axPlayer_OnStateChanged(object sender, AxAPlayer3Lib._IPlayerEvents_OnStateChangedEvent e)
        {
            if (e.nNewState == 0)
            {
                Stop();
            }
        }
        #endregion

        #region 声音操作
        bool IsVolumeMouseDown = false;
        private void dmVolumeProgress_MouseDown(object sender, MouseEventArgs e)
        {
            //进度条鼠标按下
            if (dmVolumeProgress.Cursor == Cursors.Hand)
            {
                IsVolumeMouseDown = true;
            }
        }

        private void dmVolumeProgress_MouseMove(object sender, MouseEventArgs e)
        {
            //鼠标拖动
            if (IsVolumeMouseDown)//鼠标按下
            {
                SetVolume(Convert.ToInt32(dmVolumeProgress.DM_Value));
            }
        }
       

        private void dmVolumeProgress_MouseUp(object sender, MouseEventArgs e)
        {
            //进度条鼠标抬起
            IsVolumeMouseDown = false;
            SetVolume(Convert.ToInt32(dmVolumeProgress.DM_Value));
        }
        /// <summary>
        /// 缓冲
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void axPlayer_OnBuffer(object sender, AxAPlayer3Lib._IPlayerEvents_OnBufferEvent e)
        {
            dmProgressBar.DM_BufferValue = e.nPercent;
        }
        #endregion


实例下载地址

DMPlayer播放器 完整源代码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警