【WinForm】WinForm实现音乐播放器

前言


一、效果

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
在这里插入图片描述

二、界面设计

注意要调用一个音频库文件:
在这里插入图片描述
命名空间中引用:

using WMPLib;
using System.Media;
using System.IO;
using System.Threading;

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三、代码

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 WMPLib;
using System.Media;
using System.IO;
using System.Threading;

namespace 音乐播放器
{
    public partial class Form2 : Form
    {
        public int index = 1;
        public int listIndex;
        SoundPlayer player = new SoundPlayer();
        Dictionary<string, string> dic = new Dictionary<string, string>();
        IWMPMedia media;
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// 导入文件
        /// </summary>
        private void tsmiLoading_Click(object sender, EventArgs e)
        {
            string fileName, fileExtension, fileSize, temp;
            FileInfo fi = null;
            ListViewItem lvi = null;

            openFileDialog1.Multiselect = true;
            openFileDialog1.Filter = "mp3文件(*.mp3)|*.mp3|wav文件(*.wav)|*.wav|wma文件(*.wma)|*.wma";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                foreach (string filePath in openFileDialog1.FileNames)
                {
                    fi = new FileInfo(filePath);
                    temp = filePath.Remove(filePath.LastIndexOf('.'));
                    fileName = temp.Remove(0, temp.LastIndexOf('\\') + 1);
                    fileExtension = filePath.Remove(0, filePath.LastIndexOf('.'));
                    fileSize = (fi.Length / 1024).ToString() + "KB";

                    lvi = new ListViewItem();
                    lvi.Text = index++.ToString();
                    lvi.SubItems.AddRange(new string[] { fileName, fileExtension, fileSize, filePath });

                    if (lvDetail.Items.Count > 0)
                    {
                        if (!dic.ContainsKey(filePath))
                        {
                            lvDetail.Items.Add(lvi);
                            dic.Add(filePath, fileName);
                            //添加到播放列表
                            media = wmp.newMedia(filePath);
                            wmp.currentPlaylist.insertItem(listIndex++, media);
                        }
                        else
                        {
                            index--;
                            MessageBox.Show("文件已存在!");
                        }
                    }
                    else
                    {
                        lvDetail.Items.Add(lvi);
                        dic.Add(filePath, fileName);
                        //添加到播放列表
                        media = wmp.newMedia(filePath);
                        wmp.currentPlaylist.insertItem(listIndex++, media);
                    }
                }
            }
        }

        /// <summary>
        /// 清除列表
        /// </summary>
        private void tsmiClear_Click(object sender, EventArgs e)
        {
            lvDetail.Items.Clear();
        }

        /// <summary>
        /// 播放
        /// </summary>
        private void tsmiPlayer_Click(object sender, EventArgs e)
        {
            wmp.Ctlcontrols.play();
        }

        /// <summary>
        /// 暂停
        /// </summary>
        private void tsmiWaiting_Click(object sender, EventArgs e)
        {
            wmp.Ctlcontrols.pause();
        }

        /// <summary>
        /// 停止
        /// </summary>
        private void tsmiStop_Click(object sender, EventArgs e)
        {
            wmp.Ctlcontrols.stop();
        }

        /// <summary>
        /// 退出
        /// </summary>
        private void tsmiExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// 双击某项播放
        /// </summary>
        private void lvDetail_DoubleClick(object sender, EventArgs e)
        {
            if (lvDetail.SelectedItems.Count > 0)
            {
                wmp.currentMedia = wmp.currentPlaylist.Item[int.Parse(lvDetail.SelectedItems[0].Text) - 1];
            }
        }
    }
}


总结

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值