在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#多媒体编程 → Windows Media Player开发音乐播放器 示例源码

Windows Media Player开发音乐播放器 示例源码

C#多媒体编程

下载此实例
  • 开发语言:C#
  • 实例大小:39.59M
  • 下载次数:44
  • 浏览次数:1101
  • 发布时间:2018-03-06
  • 实例类别:C#多媒体编程
  • 发 布 人:Dandelion_liu
  • 文件格式:.7z
  • 所需积分:2
 相关标签: MediaPlayer windows

实例介绍

【实例简介】windowsmediaplayer自带方法写的,xml的操作实现增删查

【实例截图】


from clipboard


需要启用 Windows Media Player 功能,否则会提示【System.Runtime.InteropServices.COMException:“没有注册类】

操作步骤:控制面板>>程序>>启用或者关闭windows 功能>>选中 媒体功能>>Windows Media Player,点击确定即可

from clipboard

【核心代码】


using CCWin;
using MediaControlLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace 音乐播放器
{
    public partial class Form1 : CCSkinMain
    {
        string path = "";//保存默认路径
        [DllImport("user32.dll")]
        private static extern bool AnimateWindow(IntPtr handle, int ms, int flags);
        public const Int32 AW_HOR_POSITIVE = 0x00000001;
        public const Int32 AW_HOR_NEGATIVE = 0x00000002;
        public const Int32 AW_VER_POSITIVE = 0x00000004;
        public const Int32 AW_VER_NEGATIVE = 0x00000008;
        public const Int32 AW_CENTER = 0x00000010;
        public const Int32 AW_HIDE = 0x00010000;
        public const Int32 AW_ACTIVATE = 0x00020000;
        public const Int32 AW_SLIDE = 0x00040000;
        public const Int32 AW_BLEND = 0x00080000;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AnimateWindow(this.Handle, 150, AW_HOR_POSITIVE);//加载窗体动画
            XDocument x = XDocument.Load(@"File.xml");//加载Xml文件
            XElement root = x.Root;//获取根目录
            path = root.Element("Directry").Value;//加载默认路径
            label1.Text = root.Element("RecentlyMuiscename").Value;//加载上次关闭窗体的歌名
            skinTrackBar1.Value = Convert.ToUInt16(root.Element("Muicesvoid").Value);//加载上次关闭窗体音量
            foreach (String name in Directory.GetFiles(path))//遍历默认路径的文件夹里的Wav格式的歌
            {
                String jj = Path.GetFileName(name);
                if (jj.Substring(jj.Length - 4).Equals(".wav") || jj.Substring(jj.Length - 4).Equals(".mp3"))
                {
                    listBox1.Items.Add(jj);
                }
            }
            XElement XE = root.Element("Muices");
            foreach(var item in XE.Elements("MuicesFile")) { listBox2.Items.Add(item.Attribute("id").Value);}//遍历最近歌单
            foreach (var item in x.Root.Element("yin").Elements("MuicesFile").Attributes("id")){ listBox4.Items.Add(item.Value); }//遍历电音
            foreach (var item in x.Root.Element("mylike").Elements("MuicesFile").Attributes("id")){listBox3.Items.Add(item.Value);  }//遍历我喜欢的
            this.Text = label1.Text;//歌名设置为窗体标题
            axWindowsMediaPlayer1.URL = path   "\\"   label1.Text;//加载第一首歌的歌曲
            axWindowsMediaPlayer1.Ctlcontrols.stop();
            axWindowsMediaPlayer1.uiMode = "None";//显示样式没有
            axWindowsMediaPlayer1.enableContextMenu = false;//禁用菜单
            if (label1.Text.Trim().Length > 20) { timer1.Enabled = true; }//字体超过20个开起走马灯
        }
        private void skinButton4_Click(object sender, EventArgs e)
        {
            skinButton5.Visible = true;
            skinButton4.Visible = false;
            axWindowsMediaPlayer1.Ctlcontrols.play();//播放音乐
        }

        private void skinButton5_Click(object sender, EventArgs e)
        {
            skinButton5.Visible = false;
            skinButton4.Visible = true;
            axWindowsMediaPlayer1.Ctlcontrols.pause();//暂停
        }
        int op = 1;
        private void skinButton6_Click(object sender, EventArgs e)//音量按钮
        {
            if (op == 1)
            {
                skinTrackBar1.Visible = true;
                op  ;
            }
            else
            {
                skinTrackBar1.Visible = false;
                op = 1;
            }

        }
        private void skinButton7_Click(object sender, EventArgs e)//收缩按钮
        {
            skinButton8.Visible = true;
            skinButton7.Visible = false;
            this.Size = new Size(this.Size.Width, 135);
            this.Opacity = 0.8;
        }

        private void skinButton8_Click(object sender, EventArgs e)//恢复原来的宽度
        {
            this.Size = new Size(this.Size.Width, 490);
            skinButton8.Visible = false;
            skinButton7.Visible = true;
            this.Opacity = 1;
        }

        private void skinButton9_Click(object sender, EventArgs e)//打开
        {
            FolderBrowserDialog dia = new FolderBrowserDialog();
            if (dia.ShowDialog() == DialogResult.OK)//选择默认路径
            {
                path = dia.SelectedPath;//保存默认文件的路径
                XDocument xx = XDocument.Load("File.xml");
                XElement xe = xx.Root;
                xe.Element("Directry").Value = path;
                xe.Save("File.xml");
            }
            listBox1.Items.Clear();
            foreach (String name in Directory.GetFiles(path))
            {
                String jj = Path.GetFileName(name);
                if (jj.Substring(jj.Length - 4).Equals(".wav"))
                {
                    listBox1.Items.Add(jj);
                }
            }
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            AnimateWindow(this.Handle, 100, AW_HIDE   AW_HOR_NEGATIVE);
        }
        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)//播放添加并添加到最近歌
        {
            NewMethod();
        }

        private void NewMethod()//播放音乐
        {
            progressBarControl1.Text = 0   "";
            label1.Location = new Point(-1, -3);
            timer1.Enabled = false;
            axWindowsMediaPlayer1.URL = path   "\\"   listBox1.SelectedItem.ToString();
            label3.Text = axWindowsMediaPlayer1.currentMedia.durationString;//显示音频文件的时间
            label1.Text = listBox1.SelectedItem.ToString();
            skinButton5.Visible = true;
            skinButton4.Visible = false;
            this.Text = label1.Text;
            if (label1.Text.Trim().Length > 20)
            {
                timer1.Enabled = true;
            }
            listBox2.Items.Remove(listBox1.Text);
            listBox2.Items.Add(listBox1.Text);
            XDocument xdocument = XDocument.Load("File.xml");
            XElement root = xdocument.Root;
            XElement muisces = root.Element("Muices");//获取最近播放的目录
            try
            {
                XElement ie = muisces.Elements("MuicesFile").Where(elements => elements.Attribute("id").Value.ToString() == label1.Text).Single();//目录有就删除
                if (ie != null) { ie.Remove(); }
            }
            catch(InvalidOperationException SS)
            {

            }
            XElement xmuices = new XElement("MuicesFile");
            xmuices.SetAttributeValue("id", listBox1.SelectedItem.ToString());
            XElement xFile = new XElement("File", path   "\\"   listBox1.SelectedItem.ToString());
            xmuices.Add(xFile);
            muisces.Add(xmuices);
            xdocument.Save("File.xml");
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)//关闭最后保存的信息
        {
            XDocument xx = XDocument.Load("File.xml");
            XElement xe = xx.Root;
            xe.Element("RecentlyMuiscename").Value = label1.Text;
            xe.Element("Muicesvoid").Value = axWindowsMediaPlayer1.settings.volume   "";
            xe.Save("File.xml");
        }

        private void skinButton2_Click_1(object sender, EventArgs e)//下一首
        {
            if (listBox1.Focus() == true)
            {
                listBox1.SelectedIndex = listBox1.SelectedIndex == listBox1.Items.Count - 1 ? 0 :   listBox1.SelectedIndex;
                NewMethod();
            }
            else if(listBox2.Focus()==true)
            {
                listBox2.SelectedIndex = listBox2.SelectedIndex == listBox2.Items.Count - 1 ? 0 :   listBox2.SelectedIndex;
                NewMethod2();
            }
            else if (listBox3.Focus() == true)
            {
                listBox3.SelectedIndex = listBox3.SelectedIndex == listBox3.Items.Count - 1 ? 0 :   listBox3.SelectedIndex;
                NewMethod3();
            }
            else if (listBox4.Focus() == true)
            {
                listBox4.SelectedIndex = listBox4.SelectedIndex == listBox4.Items.Count - 1 ? 0 :   listBox4.SelectedIndex;
                NewMethod4();
            }
            skinButton4.Visible = false;
            skinButton5.Visible = true;
        }

        private void NewMethod2()//播放二的方法
        {
            XDocument xd = XDocument.Load("File.xml");
            XElement xdt = xd.Root.Element("Muices");
            XElement xe = xdt.Elements("MuicesFile").Where(x => x.Attribute("id").Value == listBox2.SelectedItem.ToString()).Single();
            axWindowsMediaPlayer1.URL = xe.Value;
            label1.Text = listBox2.SelectedItem.ToString();
            skinButton4.Visible = false;
            skinButton5.Visible = true;
        }

        private void NewMethod3()//播放三的方法
        {
            XDocument xd = XDocument.Load("File.xml");
            XElement xdt = xd.Root.Element("mylike");
            XElement xe = xdt.Elements("MuicesFile").Where(x => x.Attribute("id").Value == listBox3.SelectedItem.ToString()).Single();
            axWindowsMediaPlayer1.URL = xe.Value;
            label1.Text = listBox3.SelectedItem.ToString();
            skinButton4.Visible = false;
            skinButton5.Visible = true;
        }

        private void NewMethod4()//播放四的方法
        {
            XDocument xd = XDocument.Load("File.xml");
            XElement xdt = xd.Root.Element("yin");
            XElement xe = xdt.Elements("MuicesFile").Where(x => x.Attribute("id").Value == listBox4.SelectedItem.ToString()).Single();
            axWindowsMediaPlayer1.URL = xe.Value;
            label1.Text = listBox4.SelectedItem.ToString();
            skinButton4.Visible = false;
            skinButton5.Visible = true;
        }

        private void skinButton3_Click(object sender, EventArgs e)//上一首
        {
            if (listBox1.Focus() == true)
            {
                listBox1.SelectedIndex = listBox1.SelectedIndex == 0 ? listBox1.Items.Count - 1 : --listBox1.SelectedIndex;
                NewMethod();
            }
            else if (listBox2.Focus() == true)
            {
                listBox2.SelectedIndex = listBox2.SelectedIndex == 0 ? listBox2.Items.Count - 1 : --listBox2.SelectedIndex;
                NewMethod2();
            }
            else if (listBox3.Focus() == true)
            {
                listBox3.SelectedIndex = listBox3.SelectedIndex == 0 ? listBox3.Items.Count - 1 : --listBox3.SelectedIndex;
                NewMethod3();
            }
            else if (listBox4.Focus() == true)
            {
                listBox4.SelectedIndex = listBox4.SelectedIndex == 0 ? listBox4.Items.Count - 1 : --listBox4.SelectedIndex;
                NewMethod4();
            }      
        }

        private void timer1_Tick(object sender, EventArgs e)//走马灯
        {
            label1.Location = new Point(label1.Location.X - 1, label1.Location.Y);
            if (label1.Right <= 0)
            {
                label1.Location = new Point(-1, -3);
            }
        }

        private void 我喜欢的音乐ToolStripMenuItem_Click(object sender, EventArgs e)//添加到我喜欢的歌单
        {
            listBox3.Items.Add(listBox1.SelectedItem.ToString());
            XDocument x = XDocument.Load("File.xml");
            XElement xl=x.Root.Element("mylike");
            XElement file1 = new XElement("MuicesFile",new XElement("File",path "\\" listBox1.SelectedItem.ToString()));
            file1.SetAttributeValue("id", listBox1.SelectedItem.ToString());
            xl.Add(file1);
            x.Save("File.xml");
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            label4.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPositionString;
            if (!string.IsNullOrEmpty(label4.Text))
            {
                label3.Text = axWindowsMediaPlayer1.currentMedia.durationString;//显示音频文件的时间
                progressBarControl1.Text = (axWindowsMediaPlayer1.Ctlcontrols.currentPosition / axWindowsMediaPlayer1.currentMedia.duration).ToString("0%").Split('%')[0];//百分比显示进度条  
            }
            if (progressBarControl1.Text == "100")
            {
                if (listBox1.Focus() == true)
                {
                    listBox1.SelectedIndex = listBox1.SelectedIndex == listBox1.Items.Count - 1 ? 0 :   listBox1.SelectedIndex;
                    NewMethod();
                }
                else if (listBox2.Focus() == true)
                {
                    listBox2.SelectedIndex = listBox2.SelectedIndex == listBox2.Items.Count - 1 ? 0 :   listBox2.SelectedIndex;
                    NewMethod2();
                }
                else if (listBox3.Focus() == true)
                {
                    listBox3.SelectedIndex = listBox3.SelectedIndex == listBox3.Items.Count - 1 ? 0 :   listBox3.SelectedIndex;
                    NewMethod3();
                }
                else if (listBox4.Focus() == true)
                {
                    listBox4.SelectedIndex = listBox4.SelectedIndex == listBox4.Items.Count - 1 ? 0 :   listBox4.SelectedIndex;
                    NewMethod4();
                }
            }
        }

        private void skinTrackBar1_Scroll(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.settings.volume = skinTrackBar1.Value;//播放器声音设置
        }

        private void skinButton10_Click(object sender, EventArgs e)
        {
            MessageBox.Show((axWindowsMediaPlayer1.Ctlcontrols.currentPosition /axWindowsMediaPlayer1.currentMedia.duration).ToString("0%"));
        }

        private void 播放ToolStripMenuItem_Click(object sender, EventArgs e)//菜单播放音乐
        {
            NewMethod();
        }

        private void 从列表删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listBox2.Items.Remove(listBox1.SelectedItem.ToString());//从列表删除
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Directory.Delete(path   "\\"   listBox1.SelectedItem.ToString());//删除路径文件
        }

        private void listBox2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            XDocument x = XDocument.Load("File.xml");
            XElement xl=x.Root.Element("Muices").Elements("MuicesFile").Where(ss=>ss.Attribute("id").Value.ToString()==listBox2.SelectedItem.ToString()).Single();
            axWindowsMediaPlayer1.URL=xl.Element("File").Value;
            skinButton4.Visible = false;
            skinButton5.Visible = true;
        }
        bool c1 = true;
        private void button1_Click(object sender, EventArgs e)
        {
            if (c1){
            listBox3.Visible = true;
            c1 = false;
            }else{
            listBox3.Visible = false;
            c1 = true;
            }
        }
        bool tt = true;
        private void button2_Click(object sender, EventArgs e)
        {
            if (tt)
            {
                button1.Visible = false;
                listBox4.Visible = true;
                button2.Location = new Point(0, 0);
                tt = false;
            }
            else
            {
                button1.Visible = true;
                listBox4.Visible = false;
                button2.Location = new Point(0, 22);
                tt = true;
            }

        }

        private void 电音ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listBox4.Items.Add(listBox1.SelectedItem.ToString());
            XDocument x = XDocument.Load("File.xml");
            XElement xl = x.Root.Element("yin");
            XElement file1 = new XElement("MuicesFile", new XElement("File", path   "\\"   listBox1.SelectedItem.ToString()));
            file1.SetAttributeValue("id", listBox1.SelectedItem.ToString());
            xl.Add(file1);
            x.Save("File.xml");
        }

        private void listBox4_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            NewMethod4();
        }

        private void listBox3_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            NewMethod3();
        }
    }
}


标签: MediaPlayer windows

实例下载地址

Windows Media Player开发音乐播放器 示例源码

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

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

网友评论

第 1 楼 zhanglc8801 发表于: 2018-03-07 20:18 17
不能播放无损音乐格式的播放器不是一个好的播放器。

支持(0) 盖楼(回复)

发表评论

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

查看所有1条评论>>

小贴士

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

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

关于好例子网

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

;
报警