目录
功能预览
- 选择并导入音乐文件
- 双击一首,开始播放
- 播放/暂停键
- 停止键
- 上一首/下一首
- 静音
- 右键删除列表中的某首音乐/批量删除列表中的音乐
- 更改软件背景

Step1.基本功能(选择/播放/暂停/停止)
- 工具箱-Components-WindowsMediaPlayer,拖拽其到Form中,调整至合适大小
- 拖拽一个listBox,用于储存音乐列表;设置“选择”/“播放/暂停”/“停止”button
- 选择音乐功能
3.1
设置一个“打开文件窗口”,分别设置其初始打开地址/窗口名称/文件过滤器/多选选项
3.2
分别设置listPath和path数组
3.3
Ofd.FileName此属性返回选中文件全路径。适用选中一个文件,如果是多个文件就用 FileNames,用数组接收。用path接收文件名
3.4
![]() | |
| 用listPath接收文件的全路径,用path为listBox添加Items |
3.5设置双击歌曲,将播放器的URL设置为此歌曲的全路径

4.
musicPlayer.Ctlcontrols.play();
musicPlayer.Ctlcontrols.pause();
musicPlayer.Ctlcontrols.stop();
用于控制播放/暂停/停止
Step2.上一首/下一首
-
2. 上一首下一首 

Step3.右键删除音乐

Step4.更改软件背景图片
- 设置几个预选项的按钮
-

代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace MusicPlayer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/*
private void button1_Click(object sender, EventArgs e)
{
musicPlayer.Ctlcontrols.play();
}
private void button2_Click(object sender, EventArgs e)
{
musicPlayer.Ctlcontrols.pause();
}*/
private void button3_Click(object sender, EventArgs e)
{
musicPlayer.Ctlcontrols.stop();
}
private void Form1_Load(object sender, EventArgs e)
{
//取消自动播放
musicPlayer.settings.autoStart = false;
BackgroundImage = Image.FromFile("Media\\bk_moon.jpg");
}
bool b = true;
private void btnPlayorPause_Click(object sender, EventArgs e)
{
if(btnPlayorPause .Text =="播放")
{
if(b==true)
{
musicPlayer.URL=listPath[listBox1.SelectedIndex];
}
//获得选中歌曲,让音乐重头播放
musicPlayer.Ctlcontrols.play();
btnPlayorPause.Text = "暂停";
}
else if(btnPlayorPause.Text == "暂停")
{
musicPlayer.Ctlcontrols.pause();
btnPlayorPause.Text = "播放";
b = false;
}
}
List<string> listPath = new List<string>();
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = "D://";
ofd.Filter ="音乐文件|*.wav|MP3文件|*.mp3|所有文件|*.*";
ofd.Title = "请选择音乐文件";
ofd.Multiselect = true;//允许多选
ofd.ShowDialog();
//获得文本框中选择文件的全路径
string[] path = ofd.FileNames;
for(int i=0;i<path.Length;i++)
{
//将音乐文件的全路径存储到泛型集合中
listPath.Add(path[i]);
//将音乐文件名存储到Listbox中
listBox1.Items.Add(Path.GetFileName( path[i]));
}
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
//双击播放
if(listBox1 .Items .Count ==0)
{
MessageBox.Show("请先选择音乐");
return;
}
try
{
musicPlayer.URL = listPath[listBox1.SelectedIndex];
musicPlayer.Ctlcontrols.play();
btnPlayorPause.Text = "暂停";
}
catch { }
}
private void btnGreen_Click(object sender, EventArgs e)
{
BackgroundImage = Image.FromFile("Media\\bk_couple.jpg");
}
private void btnRed_Click(object sender, EventArgs e)
{
BackgroundImage = Image.FromFile("Media\\bk_red.jpg");
}
private void btnBlue_Click(object sender, EventArgs e)
{
BackgroundImage = Image.FromFile("Media\\bk_moon.jpg");
}
private void btnBlack_Click(object sender, EventArgs e)
{
BackgroundImage = Image.FromFile("Media\\bk_mountain.jpg");
}
private void button1_Click_1(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
index--;
if (index <0)
{
index = listBox1.Items.Count-1;
}
//将选中清空,下移
listBox1.SelectedIndices.Clear();
listBox1.SelectedIndex = index;
musicPlayer.URL = listPath[index];
musicPlayer.Ctlcontrols.play();
}
private void button2_Click_1(object sender, EventArgs e)
{
//点击下一曲
//先获得当前索引
int index = listBox1.SelectedIndex;
index++;
if(index ==listBox1 .Items .Count )
{
index = 0;
}
//将选中清空,下移
listBox1.SelectedIndices.Clear();
listBox1.SelectedIndex = index;
musicPlayer.URL = listPath[index];
musicPlayer.Ctlcontrols.play();
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
//点击删除选中歌曲
//列表中的选中项
//ListPath也要删
//先删集合再删列表
//先获得要删除几首歌曲
int count = listBox1.SelectedItems.Count;
for(int i=0;i<count;i++)
{
listPath.RemoveAt(listBox1.SelectedIndex);
//再删列表
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
}
private void button5_Click(object sender, EventArgs e)
{
if (button5.Text == "+")
{
musicPlayer.settings .mute =true;
button5.Text = "-";
button5.BackColor = Color.Coral;
}
else if (button5.Text == "-")
{
musicPlayer.settings .mute=false;
button5.Text = "+";
button5.BackColor = Color.Aquamarine;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
//如果播放器的状态为正在播放中
if(musicPlayer.playState ==WMPLib.WMPPlayState.wmppsPlaying)
{
if(musicPlayer .currentMedia .durationString==musicPlayer .Ctlcontrols .currentPositionString )
{
//点击下一曲
//先获得当前索引
int index = listBox1.SelectedIndex;
index++;
if (index == listBox1.Items.Count)
{
index = 0;
}
//将选中清空,下移
listBox1.SelectedIndices.Clear();
listBox1.SelectedIndex = index;
musicPlayer.URL = listPath[index];
musicPlayer.Ctlcontrols.play();
}
}
}
}
}

705

被折叠的 条评论
为什么被折叠?



