C# 常用控件项目(音乐播放器)


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;

namespace 音乐播放器
{
public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
}

//添加音乐文件
private void btn_add_Click(object sender, EventArgs e)
{
  //实例化一个文件对话框
  OpenFileDialog fd = new OpenFileDialog();

  //设置对话框属性:标题
  fd.Title = "选择音乐文件";

  //设置文件扩展名筛选
  fd.Filter = "(音乐文件).mp3.mp4.wav|*.mp3;*.mp4;*.wav;";

  //设置多选
  fd.Multiselect = true;

  //显示对话框
  DialogResult dr = fd.ShowDialog();
  if (dr == DialogResult.OK) //点击的是确定按钮
    {
    //获取所有选择的音乐文件名--数组
    string[] fns = fd.FileNames;

    //把选择的音乐文件名添加到列表框ListBox
    foreach (string fn in fns)
    {
    this.listBox1.Items.Add(fn);
    }
  }

}

private void btn_bf_Click(object sender, EventArgs e)
{
  if (this.listBox1.SelectedIndex == -1)
  { //如果没有选中歌曲,默认选择第一首
    this.listBox1.SelectedIndex = 0;
  }
  //播放音乐 = 获取列表框的当前选中项
    this.wmp_mm.URL = this.listBox1.SelectedItem.ToString();

    //启动定时器
  this.timer1.Start();

}

//时时刻刻检测播放器是否播放完毕
  int s = 0;
private void timer1_Tick(object sender, EventArgs e)
{
  //设置进度条的进度
  if (s <= this.progressBar1.Maximum) //不能超过进度条的上限
  {
    this.progressBar1.Value = s++;
  }


  //如果播放器的播放状态为停止状态 Ctrl+J
  if (this.wmp_mm.playState == WMPLib.WMPPlayState.wmppsStopped)
  {
  //设置列表框选中下一个
  int xb = this.listBox1.SelectedIndex;
  xb++;
    if (xb == this.listBox1.Items.Count)
    { //如果到了最后一首,回到第一首
      xb = 0;
    }
  this.listBox1.SelectedIndex = xb;

  //设置播放器URL
  this.wmp_mm.URL = this.listBox1.SelectedItem.ToString();
  }
}

 

}
}

//因为工具箱没有音乐播放工具 我们要导入windows自带的播放器

添加播放器控件说明:
第一步:添加选项卡-为选项卡命名
第二步:在这个选项卡里面右键->选择项
第三步:找到COM组件->Windows Media Player

//窗体UI设计

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM