实例介绍
【实例简介】
【实例截图】
【核心代码】
namespace musicplayer
{
public partial class Form1 : Form
{
//声明一个list,用来存储文件的路径
List<string> urlList = new List<string>();//泛型
double max, min;
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
/// <summary>
/// 添加歌曲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnInput_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
//让选择器可以选择多个文件
of.Multiselect = true;
of.Title = "请选择音乐文件";
//指定选择文件的类型
//方法一
//of.Filter = "音乐文件|*.mp3|WAV文件|所有文件|*.*";
//方法二
of.Filter = "Mp3文件|*.mp3|Wav文件|*.wav|Wma文件|*.wma|Wmv文件|*.wmv|所有格式|*.*";
//方法三
//of.Filter = "(*.mp3)|*.mp3";
//确认用户选择的是确认按钮
//of.ShowDialog();
if (of.ShowDialog() == DialogResult.OK)
{
//把用户选择的文件存储到数组中
string[] nameList = of.FileNames;
//读取数组中的数据
foreach (string url in nameList)
{
//Path.GetFileNameWithoutExtension(url)获取具有扩展名的文件名
listBoxMusics.Items.Add(Path.GetFileName(url));
urlList.Add(url);
}
}
}
/// <summary>
/// 播放
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnplay_Click(object sender, EventArgs e)
{
//声明变量
int selectedIndex = listBoxMusics.SelectedIndex;
if (selectedIndex < 0)
{
//判断列表中是否有选中的歌曲,有的话播放选中的,没有的话,播放第一首
selectedIndex = selectedIndex < 0 ? 0 : selectedIndex;
//更新选中行,重新设置当前选中行的索引
listBoxMusics.SelectedIndex = selectedIndex;
//把urlList存储的url地址赋给播放器组件
axWindowsMediaPlayer1.URL = urlList[selectedIndex];
iblMusicName.Text = listBoxMusics.SelectedItem.ToString();
}
else
{
axWindowsMediaPlayer1.Ctlcontrols.play();
}
timer1.Enabled = true;
}
/// <summary>
/// 暂停
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnpause_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
/// <summary>
/// 停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnstop_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
//歌曲停止后禁用进度条
timer1.Enabled = false;
}
/// <summary>
/// 列表选择发生变化时,播放选中歌曲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBoxMusics_SelectedIndexChanged(object sender, EventArgs e)
{
//listBox中的获取和urlList相对应
//获取当前选中的索引
int selectedIndex = listBoxMusics.SelectedIndex;
//把urlList存储的url地址赋给播放器组件
axWindowsMediaPlayer1.URL = urlList[selectedIndex];
iblMusicName.Text = listBoxMusics.SelectedItem.ToString();
timer1.Enabled = true;
}
/// <summary>
/// 上一首
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLast_Click(object sender, EventArgs e)
{
//listBox中的获取和urlList相对应
//获取当前选中的索引
int selectedIndex = listBoxMusics.SelectedIndex - 1;
selectedIndex = selectedIndex < 0 ? 0 : selectedIndex;//三元算符
//更新选中行,重新设置当前选中行的索引
listBoxMusics.SelectedIndex = selectedIndex;
//把urlList存储的url地址赋给播放器组件
axWindowsMediaPlayer1.URL = urlList[selectedIndex];
iblMusicName.Text = listBoxMusics.SelectedItem.ToString();
}
/// <summary>
/// 下一首
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnNext_Click(object sender, EventArgs e)
{
//listBox中的获取和urlList相对应
//获取当前选中的索引
int selectedIndex = listBoxMusics.SelectedIndex 1;
selectedIndex = selectedIndex == listBoxMusics.Items.Count ? listBoxMusics.SelectedIndex : selectedIndex;//三元元算符
//更新选中行,重新设置当前选中行的索引
listBoxMusics.SelectedIndex = selectedIndex;
//把urlList存储的url地址赋给播放器组件
axWindowsMediaPlayer1.URL = urlList[selectedIndex];
iblMusicName.Text = listBoxMusics.SelectedItem.ToString();
}
/// <summary>
/// 进度条控制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
//获取文件的长度
max = axWindowsMediaPlayer1.currentMedia.duration;
//获取当前歌曲的播放位置
min = axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
//类型强转
trackBar1.Maximum = (int)(max);
trackBar1.Value = (int)(min);
//一首歌播放完成后,继续下一首
if (axWindowsMediaPlayer1.playState == WMPPlayState.wmppsStopped)
{
//当前歌曲播放结束后,要获取下一首歌曲的索引值
int SelectedIndex = listBoxMusics.SelectedIndex 1;
SelectedIndex = SelectedIndex == listBoxMusics.Items.Count ? 0 : SelectedIndex;//三元元算符
axWindowsMediaPlayer1.URL = urlList[SelectedIndex];
listBoxMusics.SelectedIndex = SelectedIndex;
iblMusicName.Text = listBoxMusics.SelectedItem.ToString();
trackBar1.Value = 0;
timer1.Enabled = true;
}
}
/// <summary>
/// 鼠标按下
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trackBar1_MouseDown(object sender, MouseEventArgs e)
{
//暂停播放
timer1.Enabled = false;
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
/// <summary>
/// 鼠标抬起
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trackBar1_MouseUp(object sender, MouseEventArgs e)
{
double dovalue = trackBar1.Value;//获取被拖动以后的位置
axWindowsMediaPlayer1.Ctlcontrols.currentPosition = dovalue;//重置播放位置
axWindowsMediaPlayer1.Ctlcontrols.play();
timer1.Enabled = true;
}
public int SelectedIndex { get; set; }
private void trackBar1_Scroll(object sender, EventArgs e)
{
}
/// <summary>
/// 窗口加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{ //窗体启动最大化
//this.WindowState = FormWindowState.Maximized;
//程序加载更换皮肤
skinEngine1.SkinFile = @"D:\documents\visual studio 2012\Projects\musicplayer\musicplayer\bin\Debug\Debug\skin\DiamondGreen.ssk";
//设置图片在pictureBox1布局,使图片适应大小
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
private void button1_Click(object sender, EventArgs e)
{
}
/// <summary>
/// 图片自动播放
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer2_Tick(object sender, EventArgs e)
{
string[] imgspath = Directory.GetFiles(@"C:\Users\Administrator\Desktop\lmages");
i ;
if (i == imgspath.Length)
{
i = 0;
}
pictureBox1.Image = Image.FromFile(imgspath[i]);
}
public int i { get; set; }
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
/// <summary>
/// 删除事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (listBoxMusics.SelectedIndex == -1)
{
return;
}
listBoxMusics.Items.Remove(listBoxMusics.SelectedItem);
}
/// <summary>
/// 清空列表事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 清空ToolStripMenuItem_Click(object sender, EventArgs e)
{
//清空之前先提示用户是否确认清空
if(MessageBox.Show("是否清空列表?")==DialogResult.OK)
{
listBoxMusics.Items.Clear();//清空
}
}
/// <summary>
/// 音量控制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trackBar2_Scroll(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.volume = trackBar2.Value;//设置播放音量大小
}
/// <summary>
/// 窗体关闭前提醒
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//MessageBox.Show("窗口关闭时");
DialogResult dr;
dr = MessageBox.Show("确认退出吗", "确认对话框", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (dr == DialogResult.Yes)
{ }
else
{
e.Cancel = true;
}
}
private void label1_Click_1(object sender, EventArgs e)
{
}
}
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论