目录
功能预览
Step1.基本功能(选择/播放/暂停/停止)
Step2.上一首/下一首
Step3.右键删除音乐
Step4.更改软件背景图片
代码
功能预览
- 选择并导入音乐文件
- 双击一首,开始播放
- 播放/暂停键
- 停止键
- 上一首/下一首
- 静音
- 右键删除列表中的某首音乐/批量删除列表中的音乐
- 更改软件背景
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 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 listPath = new List();
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
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 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 listPath = new List();
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



