前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >制作音乐播放器(C#)

制作音乐播放器(C#)

作者头像
跋扈洋
发布2021-04-25 15:56:22
1.1K0
发布2021-04-25 15:56:22
举报

功能

实现音乐播放器的基本功能,可以选择文件播放,也可以拖拉文件播放。 本项目中,使用的是.wav文件。

程序

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;  //Path类用到
using System.Media;    //SoundPlayer命名空间
namespace player
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<string> listsongs = new List<string>();   //用来存储音乐文件的全路径
        private void button1_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 button2_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();
        }

        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();
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Link;
            else e.Effect = DragDropEffects.None;
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string[] filePath = (string[])e.Data.GetData(DataFormats.FileDrop);
            foreach (string file in filePath)
            {
                //file就是单个文件路径
                SoundPlayer sp = new SoundPlayer();
                sp.SoundLocation = file;
                sp.Play();
              //  MessageBox.Show(file);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SoundPlayer sp = new SoundPlayer();
            if (listBox1.SelectedIndex > 0)
                sp.SoundLocation = listsongs[listBox1.SelectedIndex];
            sp.Play();
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-04-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 物联网知识 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 功能
  • 程序
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档