【WinForm】WinForm实现音乐播放器

简介: 示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

@TOC

前言


一、效果

示例: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];
            }
        }
    }
}

总结

目录
相关文章
|
C# Windows
wpf怎么使用WindowsFormsHost(即winform控件)
原文:wpf怎么使用WindowsFormsHost(即winform控件) 使用方法:   1、首先,我们需要向项目中的引用(reference)中添加两个动态库dll,一个是.
5069 0
|
9月前
38【WinForm】WinForm常见窗体技术汇总
- 窗体调用外部程序与渐变窗体 - 按回车键跳转窗体中的光标焦点 - 剪切板操作
51 0
WinForm——ContextMenuStrip总结
WinForm——ContextMenuStrip总结
312 0
|
测试技术
艾伟:WinForm控件开发总结(二)------使用和调试自定义控件
在上一篇文章里我们创建了一个简单的控件FirstControl,现在我来介绍一下怎么使用和调试自己的控件。我希望将过程写的尽可能的详细,让想学习控件开发的朋友容易上手,高手们见谅。       在同一个solution里添加一个Windows Application工程(在Solution Explorer里右键点击CustomControlSample solution选择Add->New Project…),命名为TestControl。
864 0
|
C# Windows
使用WPF来创建 Metro UI程序
原文:使用WPF来创建 Metro UI程序 这个是我以前网上看到的一篇文章,原文地址是:Building a Metro UI with WPF,这篇文章一步步的介绍了如何实现一个Metro样式的窗口,并且效果非常好。
1234 0
|
C# Windows 安全
WinForm控件与WPF控件的交互
原文:WinForm控件与WPF控件的交互 这个问题其实也可以理解为:怎样在WPF/XAML中使用Winform中的控件(如PictureBox)?首先看看XAML代码:(注意下面加粗的部分)              ...
1050 0
|
C# Windows
使用WPF技术模拟手机界面
原文:使用WPF技术模拟手机界面 1. 前言 WPF(Windows Presentation Foundation),即“Windows呈现基础”,它的目的非常明确,就是用来把数据“显示”给用户看的(说白了就是用来做UI的)。
1579 0
|
C#
在Winform窗体中使用WPF控件(附源码)
原文:在Winform窗体中使用WPF控件(附源码) 今天是礼拜6,下雨,没有外出,闲暇就写一篇博文讲下如何在Winform中使用WPF控件。原有是我在百度上搜索相关信息无果,遂干脆动手自己实现。 WPF控件的漂亮是Winform无法匹及的,本文主旨是在Winform工程中如何使用WPF控件。
1785 0