c#基于winform制作音乐播放器

论坛 期权论坛 脚本     
niminba   2021-5-23 02:49   667   0

前言:项目是c#的winform 写的,使用的播放器是基于AxWindowsMediaPlayer。

AxWindowsMediaPlayer的方法

1 首先新建一个页面 如图所示: 图片左侧是列表 使用listview 右侧是背景图片。图片框框的地方是后面可以实现的,+和-按钮分别代表添加文件和删除文件 还有就是控制播放的顺序。下面的分别是修改歌词的字体 和展示/隐藏

2 新建一个透明的歌词页面[窗体]

3 新建一个半透明的页面[窗体]

4 业务代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WMPLib;
using System.Media;
using System.IO;
using System.Text.RegularExpressions;
using AxWMPLib;
using System.Drawing.Drawing2D;
using CCWin;


namespace KenMusicPlayer
{
 public partial class MusicPlayer : Skin_DevExpress
 {


  public int index = 1;
  public int listIndex;
  private bool first_in = true; //是否第一次进入歌词区域
  private bool showLrc = true;//默认显示歌词
  private int imageInd = 0;//播放的图片下标
  private List<string> imageList;//播放的图片
  private Point closePoint;//关闭按钮的位置
  private Size dfSize;//最初的位置


  //声音
  SoundPlayer player = new SoundPlayer();
  Dictionary<string, string> dic = new Dictionary<string, string>();

  //播放列表
  Dictionary<string, IWMPMedia> playListDict = new Dictionary<string, IWMPMedia>();

  List<string> al = new List<string>(); //当前歌词时间表  

  IWMPMedia media;

  /*
 *下面这一大段API调用,主要是用来设置歌词窗口的滚动条的
 *但其实后面,我并没有怎么用到,只是在将滚动条滚动到底部时,用了一下
 */
  private const int WM_VSCROLL = 0x115;
  private const int SB_HORZ = 0;
  private const int SB_VERT = 1;
  private const int SB_CTL = 2;
  private const int SB_BOTH = 3;
  private const int SB_LINEUP = 0;
  private const int SB_LINELEFT = 0;
  private const int SB_LINEDOWN = 1;
  private const int SB_LINERIGHT = 1;
  private const int SB_PAGEUP = 2;
  private const int SB_PAGELEFT = 2;
  private const int SB_PAGEDOWN = 3;
  private const int SB_PAGERIGHT = 3;
  private const int SB_THUMBPOSITION = 4;
  private const int SB_THUMBTRACK = 5;
  private const int SB_TOP = 6;
  private const int SB_LEFT = 6;
  private const int SB_BOTTOM = 7;
  private const int SB_RIGHT = 7;
  private const int SB_ENDSCROLL = 8;
  private const int WM_PAINT = 0x000F;


  [System.Runtime.InteropServices.DllImport("user32.dll")]
  public static extern bool ScrollWindow(IntPtr hWnd, int XAmount, int YAmount, ref Rectangle lpRect, ref Rectangle lpClipRect);

  [System.Runtime.InteropServices.DllImport("user32.dll")]
  public static extern int SetScrollPos(IntPtr hwnd, int nBar, int nPos, bool bRedraw);

  [System.Runtime.InteropServices.DllImport("user32.dll")]
  public static extern int SetScrollPos(int nBar, int nPos, bool bRedraw);

  [System.Runtime.InteropServices.DllImport("user32.dll")]
  public static extern int GetScrollPos(IntPtr hwnd, int nBar);


  [System.Runtime.InteropServices.DllImport("user32.dll")]
  public static extern bool UpdateWindow(IntPtr hWnd);

  [System.Runtime.InteropServices.DllImport("user32.dll")]
  public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

  public void setWord()
  {
   
  }


  public MusicPlayer()
  {
   this.StartPosition = FormStartPosition.CenterScreen;//窗口居中显示
   InitializeComponent();
  }


  private void MusicPlayer_Load(object sender, EventArgs e)
  {
   InitLoad();
  }


  /// <summary>
  /// 初始化 加载播放列表 如歌词 背景图 定时器等等
  /// </summary>
  private void InitLoad()
  {
   try
   {
    bool flag = false;
    string folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bgImages");
    DirectoryInfo root = new DirectoryInfo(folder);
    FileInfo[] files = root.GetFiles();
    string fileName;
    for (int i = 0; i < files.Length; i++)
    {
     fileName = files[i].Name.ToLower();
     if (fileName.EndsWith(".png") || fileName.EndsWith(".jpeg") || fileName.EndsWith(".jpg"))
     {
      if (!flag)
      {
       imageList = new List<string>();
       this.pictureBox1.Image = Image.FromFile(files[i].FullName);
      }
      imageList.Add(files[i].FullName);
      flag = true;
     }
    }

    playerType.Text = playerType.Items[0].To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B\\[XH]\^Y\\Y\\\YXZBY][B\\[XH]\^Y\\Y\\\XXZBCB\\]T[\N\\]J
N\X[XH[XY[JXЛ[XXY][K[JNCBCBB[[X\I` 9a[X\I\[HOH\\[I\[HOHH\[I]]H[]WXX\][\JCB\J
N\X[^]

NCBB]]H]\^Y\\JX\][\JCBCBHHH\N[H
KHK
H
T[HHTNKZYHKZY
H
TNH]
JN\[]K][HCB
^[^
CBK]S[Jo n.
^Y\YNCBBCBBB[[X\I.yyg#c:+*9iy/.ycb`#9/cy+#c:+9cb`#"B[X\I\[HOH\[IXXYJCB\QK][HCBCBCBOB]B9g,9g`;HYHYKK[Z\[H^\[\H[YKK[Z\B."l,y+yb-/g:gd9y/fj9+yk{&algd9y/fj9-a9z+l/c.amkl O
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP