C#实现简单音乐播放器

C#实现简单音乐播放器

1、简单音乐播放器实现

(1)新建WINFORM程序,窗体上添加三个按钮:打开:b_open;上一曲:b_up;下一曲:b_next。然后添加一个listbox控件。

要实现的功能:点击打开按钮,打开对话框选择多个音乐文件,可以将文件添加到ListBox控件中,双击ListBox控件中音乐文件名可以进行播放。点击上一曲可以切换到上一曲歌进行播放,点击下一曲可以切换到下一曲歌进行播放。

(2)为ListBox控件添加“DoubleClick”事件。

2、代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;	//Path类用到
using System.Media;    //SoundPlayer命名空间

namespace 音乐播放器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        List<string> listsongs = new List<string>();   //用来存储音乐文件的全路径

        private void b_open_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择音乐文件";      //打开对话框的标题
            ofd.InitialDirectory = @"F:\music";    //设置打开对话框的初始设置目录
            ofd.Multiselect = true; //设置多选
            ofd.Filter = @"音乐文件|*.mp3||*.wav|所有文件|*.*";    //设置文件格式筛选
            ofd.ShowDialog();   //显示打开对话框
            string[] pa_th = ofd.FileNames;       //获得在文件夹中选择的所有文件的全路径
            for (int i = 0; i < pa_th.Length;i++ )
            {
                listBox1.Items.Add(Path.GetFileName(pa_th[i]));  //将音乐文件的文件名加载到listBox中
                listsongs.Add(pa_th[i]);    //将音乐文件的全路径存储到泛型集合中
            }
        }

        //实现双击播放
        SoundPlayer sp = new SoundPlayer();
        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            SoundPlayer sp = new SoundPlayer();
            sp.SoundLocation=listsongs[listBox1.SelectedIndex];
            sp.Play();
        }

        //点击下一曲
        private void b_next_Click(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex; //获得当前选中歌曲的索引
            index++;

            if (index==listBox1.Items.Count)
            {
                index = 0;
            }
            listBox1.SelectedIndex = index; //将改变后的索引重新赋值给我当前选中项的索引
            sp.SoundLocation = listsongs[index];
            sp.Play();
        }
        //点击上一曲
        private void b_up_Click(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex; //获得当前选中歌曲的索引
            index--;

            if (index <0)
            {
                index = listBox1.Items.Count-1;
            }
            listBox1.SelectedIndex = index; //将改变后的索引重新赋值给我当前选中项的索引
            sp.SoundLocation = listsongs[index];
            sp.Play();
        }
    }
}

运行结果:


程序源代码下载:http://download.csdn.net/detail/liyuqian199695/9771151

  • 22
    点赞
  • 96
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MyQQ2010 { public partial class MusicForm : Form { public MusicForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //打开文件夹选项 OpenFileDialog f = new OpenFileDialog(); //设置能够选中的多个选项 f.Multiselect = true; //弹出文件对话框 DialogResult result = f.ShowDialog(); if ( DialogResult.OK==result) { //获取选中的歌曲的路径 string[] music = f.FileNames; //将歌曲添加到ListBox中 foreach (string item in music) { this.lstMusic.Items.Add(item); } } } //双击播放歌曲 private void lstmusic2_DoubleClick(object sender, EventArgs e) { //获取选中的歌曲 string music = this.lstmusic2.SelectedItem.ToString(); //播放歌曲 Player.URL = music; } private void 添加到播放列表ToolStripMenuItem_Click_1(object sender, EventArgs e) { if (this.lstMusic.SelectedItems.Count == 0) { MessageBox.Show("请选中你要选中的歌曲"); return; } else { //获取选中的歌曲 string music = this.lstMusic.SelectedItem.ToString(); //把选中的歌曲移动到播放列表中 this.lstmusic2.Items.Add(music); //从lstmusic中移除 this.lstMusic.Items.Remove(music); } } private void 删除音乐ToolStripMenuItem_Click_1(object sender, EventArgs e) { if (this.lstMusic.SelectedItems.Count == 0) { MessageBox.Show("请选中你要删除的歌曲"); } else { //获取选中的歌曲 string music = this.lstMusic.SelectedItem.ToString(); //把选中的歌曲删除掉 this.lstMusic.Items.Remove(music); } } private void 从列表中移除ToolStripMenuItem_Click_1(object sender, EventArgs e) { if (this.lstmusic2.SelectedItems.Count == 0) { MessageBox.Show("请选中你要删除的歌曲"); } else { //获取选中的歌曲 string music = this.lstmusic2.SelectedItem.ToString(); //把选中的歌曲删除 this.lstmusic2.Items.Remove(music); } } private void MusicForm_Load_1(object sender, EventArgs e) { //播放顺序默认为列表播放 this.cboPlayOrder.SelectedIndex = 0; this.trm1.Start(); } private void trm1_Tick(object sender, EventArgs e) { //判断歌曲播放的顺序 if (Player.playState == WMPLib.WMPPlayState.wmppsStopped) { if (this.cboPlayOrder.Text == "列表播放") { if (this.lstmusic2.SelectedIndex < this.lstmusic2.Items.Count - 1) { this.lstmusic2.SelectedIndex++; this.lstMusic.Items.Add(this.lstmusic2.SelectedItem.ToString()); Player.URL = this.lstmusic2.SelectedItem.ToString(); } else { this.lstmusic2.SelectedIndex = 0; Player.URL = this.lstmusic2.SelectedItem.ToString(); } } else if (this.cboPlayOrder.Text == "随机播放") { Random r = new Random(); int index = r.Next(0, this.lstmusic2.Items.Count - 1); this.lstmusic2.SelectedIndex = index; string item = this.lstmusic2.SelectedItem.ToString(); this.lstMusic.Items.Add(item);//随机播放的歌曲也要在lbmusic2中显示 Player.URL = item; } else { int index = this.lstmusic2.SelectedIndex; Player.URL = this.lstmusic2.SelectedItem.ToString(); } } } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值