我的第一个C#小程序之简单音乐播放器

这篇博客记录了作者初次尝试用C#编写小程序,创建了一个简单的音乐播放器。用户可以选择多个MP3文件进行播放,程序界面展示音乐文件名。源代码中使用了WMPLib库来实现音乐播放功能。
摘要由CSDN通过智能技术生成

我的第一个C#小程序之简单音乐播放器

初识C#程序

因为想自己做一些小的操作程序,以及一些互动灯光的需求,所以自学了一些基础知识,比较零碎不成系统。

第一个小程序之简单音乐播放器

界面如下图所示:

在这里插入图片描述

源代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WMPLib;

namespace Musicplayer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List songpath = new List();
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MyQQ2010 { public partial class MusicForm : Form { public MusicForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //打开文件夹选项 OpenFileDialog f = new OpenFileDialog(); //设置能够选中的多个选项 f.Multiselect = true; //弹出文件对话框 DialogResult result = f.ShowDialog(); if ( DialogResult.OK==result) { //获取选中的歌曲的路径 string[] music = f.FileNames; //将歌曲添加到ListBox中 foreach (string item in music) { this.lstMusic.Items.Add(item); } } } //双击播放歌曲 private void lstmusic2_DoubleClick(object sender, EventArgs e) { //获取选中的歌曲 string music = this.lstmusic2.SelectedItem.ToString(); //播放歌曲 Player.URL = music; } private void 添加到播放列表ToolStripMenuItem_Click_1(object sender, EventArgs e) { if (this.lstMusic.SelectedItems.Count == 0) { MessageBox.Show("请选中你要选中的歌曲"); return; } else { //获取选中的歌曲 string music = this.lstMusic.SelectedItem.ToString(); //把选中的歌曲移动到播放列表中 this.lstmusic2.Items.Add(music); //从lstmusic中移除 this.lstMusic.Items.Remove(music); } } private void 删除音乐ToolStripMenuItem_Click_1(object sender, EventArgs e) { if (this.lstMusic.SelectedItems.Count == 0) { MessageBox.Show("请选中你要删除的歌曲"); } else { //获取选中的歌曲 string music = this.lstMusic.SelectedItem.ToString(); //把选中的歌曲删除掉 this.lstMusic.Items.Remove(music); } } private void 从列表中移除ToolStripMenuItem_Click_1(object sender, EventArgs e) { if (this.lstmusic2.SelectedItems.Count == 0) { MessageBox.Show("请选中你要删除的歌曲"); } else { //获取选中的歌曲 string music = this.lstmusic2.SelectedItem.ToString(); //把选中的歌曲删除 this.lstmusic2.Items.Remove(music); } } private void MusicForm_Load_1(object sender, EventArgs e) { //播放顺序默认为列表播放 this.cboPlayOrder.SelectedIndex = 0; this.trm1.Start(); } private void trm1_Tick(object sender, EventArgs e) { //判断歌曲播放的顺序 if (Player.playState == WMPLib.WMPPlayState.wmppsStopped) { if (this.cboPlayOrder.Text == "列表播放") { if (this.lstmusic2.SelectedIndex < this.lstmusic2.Items.Count - 1) { this.lstmusic2.SelectedIndex++; this.lstMusic.Items.Add(this.lstmusic2.SelectedItem.ToString()); Player.URL = this.lstmusic2.SelectedItem.ToString(); } else { this.lstmusic2.SelectedIndex = 0; Player.URL = this.lstmusic2.SelectedItem.ToString(); } } else if (this.cboPlayOrder.Text == "随机播放") { Random r = new Random(); int index = r.Next(0, this.lstmusic2.Items.Count - 1); this.lstmusic2.SelectedIndex = index; string item = this.lstmusic2.SelectedItem.ToString(); this.lstMusic.Items.Add(item);//随机播放的歌曲也要在lbmusic2中显示 Player.URL = item; } else { int index = this.lstmusic2.SelectedIndex; Player.URL = this.lstmusic2.SelectedItem.ToString(); } } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值