基本信息
源码名称:Windows Media Player开发音乐播放器 示例源码
源码大小:39.59M
文件格式:.7z
开发语言:C#
更新时间:2018-03-06
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
windowsmediaplayer自带方法写的,xml的操作实现增删查
windowsmediaplayer自带方法写的,xml的操作实现增删查
需要启用 Windows Media Player 功能,否则会提示【System.Runtime.InteropServices.COMException:“没有注册类】
操作步骤:控制面板>>程序>>启用或者关闭windows 功能>>选中 媒体功能>>Windows Media Player,点击确定即可
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();
}
}
}