C#-窗体之音乐播放器

音乐播放器

功能:
1.选择文件夹里的音乐文件
2.根据选中的索引切换播放
3.根据索引让音乐文件显示他被选中了
4个按钮一个listbox

結果展示:

在这里插入图片描述
用来存储音乐文件的全路径 局部变量拿不到

List<string> listSongs = new List<string>();

打开按钮是选择文件路径

private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog odf = new OpenFileDialog();
            odf.Title = "选择音乐文件夹";
            odf.InitialDirectory = @"C:\Users\H0772690\Music";
            odf.Multiselect = true;//多选
            odf.Filter = "音乐文件|*.wav|所有文件|*.*";
            odf.ShowDialog();
            //获得我们在文件夹中选择的所有文件的全路径
            string[] path = odf.FileNames;
            for (int i = 0; i < path.Length; i++)
            {
                listBox1.Items.Add(path[i]);
                //将音乐文件的全路径存储到泛型集合中
                listSongs.Add(path[i]);
            }
        }

listbox

 /// <summary>
        /// 实现双击播放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
         SoundPlayer sp = new SoundPlayer();
        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            
            sp.SoundLocation = listSongs[listBox1.SelectedIndex];
            sp.Play();
        }

关闭按钮

private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

上一曲

private void button2_Click(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex;
            if (index == 0)
            {
                index = listBox1.Items.Count;
            }
            index--;
            //将改变后的索引从新赋值给我选中项的索引
            listBox1.SelectedIndex = index;
            sp.SoundLocation = listSongs[index];
            sp.Play();
        }

下一曲

private void button3_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();
        }
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔚说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值