zoukankan      html  css  js  c++  java
  • 小菜学习Winform(二)WMPLib实现音乐播放器

    前言

      现在网上有很多的音乐播放器,但好像都不是.net平台做的,在.net中实现音乐文件的播放功能很简单,下面就简单实现下。

    SoundPlayer类

      在.net提供了音乐文件的类:SoundPlayer,但是只支持.wav格式,我们用的时候一般是播放系统文件,比如一些提示声音等。代码很简单:

    1         SoundPlayer player = new SoundPlayer();
    2         player.SoundLocation = filePath;//filePath文件的物理路径
    3         player.Play();   

      使用这个类我们做个小程序,如下:

    WMP组件

      上面的小程序有几个问题:不支持暂停,进度显示,文件格式限制等,因为SoundPlayer就是来播放系统文件的,不能要求它那么全面。因为windows系统自带播放器windows media player,我们可以用使用其自带的组件来实现上面的要求,做起来也很简单。

      先引用Windows Media Player组件,然后就像拖控件一样用就行了。效果如图:

      代码实现起来很简单,播放,暂停,停止代码如下:

    1             wmp.Ctlcontrols.play();//播放
    2             wmp.Ctlcontrols.pause();//暂停
    3             wmp.Ctlcontrols.stop();//停止

      完整代码:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 
     10 using WMPLib;
     11 using System.Media;
     12 using System.IO;
     13 using System.Threading;
     14 
     15 namespace 音乐播放器
     16 {
     17     public partial class Form2 : Form
     18     {
     19         public int index = 1;
     20         public int listIndex;
     21         SoundPlayer player = new SoundPlayer();
     22         Dictionary<string, string> dic = new Dictionary<string, string>();
     23         IWMPMedia media;
     24         public Form2()
     25         {
     26             InitializeComponent();
     27         }
     28 
     29         private void Form2_Load(object sender, EventArgs e)
     30         {
     31             
     32         }
     33 
     34         /// <summary>
     35         /// 导入文件
     36         /// </summary>
     37         private void tsmiLoading_Click(object sender, EventArgs e)
     38         {
     39             string fileName, fileExtension, fileSize, temp;
     40             FileInfo fi = null;
     41             ListViewItem lvi = null;
     42 
     43             openFileDialog1.Multiselect = true;
     44             openFileDialog1.Filter = "mp3文件(*.mp3)|*.mp3|wav文件(*.wav)|*.wav|wma文件(*.wma)|*.wma";
     45             if (openFileDialog1.ShowDialog() == DialogResult.OK)
     46             {
     47                 foreach (string filePath in openFileDialog1.FileNames)
     48                 {
     49                     fi = new FileInfo(filePath);
     50                     temp = filePath.Remove(filePath.LastIndexOf('.'));
     51                     fileName = temp.Remove(0, temp.LastIndexOf('\') + 1);
     52                     fileExtension = filePath.Remove(0, filePath.LastIndexOf('.'));
     53                     fileSize = (fi.Length / 1024).ToString() + "KB";
     54 
     55                     lvi = new ListViewItem();
     56                     lvi.Text = index++.ToString();
     57                     lvi.SubItems.AddRange(new string[] { fileName, fileExtension, fileSize, filePath });
     58 
     59                     if (lvDetail.Items.Count > 0)
     60                     {
     61                         if (!dic.ContainsKey(filePath))
     62                         {
     63                             lvDetail.Items.Add(lvi);
     64                             dic.Add(filePath, fileName);
     65                             //添加到播放列表
     66                             media = wmp.newMedia(filePath);
     67                             wmp.currentPlaylist.insertItem(listIndex++, media);
     68                         }
     69                         else
     70                         {
     71                             index--;
     72                             MessageBox.Show("文件已存在!");
     73                         }
     74                     }
     75                     else
     76                     {
     77                         lvDetail.Items.Add(lvi);
     78                         dic.Add(filePath, fileName);
     79                         //添加到播放列表
     80                         media = wmp.newMedia(filePath);
     81                         wmp.currentPlaylist.insertItem(listIndex++, media);
     82                     }
     83                 }
     84             }
     85         }
     86 
     87         /// <summary>
     88         /// 清除列表
     89         /// </summary>
     90         private void tsmiClear_Click(object sender, EventArgs e)
     91         {
     92             lvDetail.Items.Clear();
     93         }
     94 
     95         /// <summary>
     96         /// 播放
     97         /// </summary>
     98         private void tsmiPlayer_Click(object sender, EventArgs e)
     99         {
    100             wmp.Ctlcontrols.play();
    101         }
    102 
    103         /// <summary>
    104         /// 暂停
    105         /// </summary>
    106         private void tsmiWaiting_Click(object sender, EventArgs e)
    107         {
    108             wmp.Ctlcontrols.pause();
    109         }
    110 
    111         /// <summary>
    112         /// 停止
    113         /// </summary>
    114         private void tsmiStop_Click(object sender, EventArgs e)
    115         {
    116             wmp.Ctlcontrols.stop();
    117         }
    118 
    119         /// <summary>
    120         /// 退出
    121         /// </summary>
    122         private void tsmiExit_Click(object sender, EventArgs e)
    123         {
    124             Application.Exit();
    125         }
    126 
    127         /// <summary>
    128         /// 双击某项播放
    129         /// </summary>
    130         private void lvDetail_DoubleClick(object sender, EventArgs e)
    131         {
    132             if (lvDetail.SelectedItems.Count > 0)
    133             {
    134                 wmp.currentMedia = wmp.currentPlaylist.Item[int.Parse(lvDetail.SelectedItems[0].Text) - 1];
    135             }
    136         }
    137     }
    138 }
    View Code

      程序下载:音乐播放器.rar

      附录:小菜学习编程-Winform系列(初学者)

  • 相关阅读:
    Oracle 数据块,文件块转换,查看数据块对像
    逆向与汇编视频(36课) IDA 使用
    VC++消息钩子编程
    VS2008中opengl配置
    BSP技术详解3有图有真相
    BSP技术详解1有图有真相
    oracle 跟踪文件和转储命令
    BSP技术详解2有图有真相
    Recognizing and Learning Object Categories 连接放送
    自学MVC看这里——全网最全ASP.NET MVC 教程汇总
  • 原文地址:https://www.cnblogs.com/xishuai/p/3519424.html
Copyright © 2011-2022 走看看