c#音乐播放器
2021-03-18 02:25
标签:ext eric text inf pat public art mes show c#音乐播放器 标签:ext eric text inf pat public art mes show 原文地址:https://www.cnblogs.com/decoct-tea/p/12369885.html
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 音乐播放器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Liststring> Music = new Liststring>();
private void button1_Click(object sender, EventArgs e)
{
//打开一个对话框,提示用户选择文件
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = ("请选择音乐文件"); //文本框标题
ofd.InitialDirectory = @"D:\FFOutput"; //默认打开的文件夹
ofd.Multiselect = true; //允许对话框选择多个文件
ofd.Filter = "音乐文件|*.wav|所有文件|*.*"; //可选的文件类型
ofd.ShowDialog(); //显示对话框
//将所有选中的文件添加到listbox中
string[] path = ofd.FileNames; //获取被选择文件的全路径
for (int i = 0; i )
{
listBox1.Items.Add(Path.GetFileName(path[i])); //将音乐文件添加到ListBox中
Music.Add(path[i]); //将音乐文件的全路径添加到List集合中
}
}
SoundPlayer sp = new SoundPlayer(); //播放对象
//双击歌曲播放
private void listBox1_DoubleClick(object sender, EventArgs e)
{
sp.SoundLocation = Music[listBox1.SelectedIndex]; //ListBox1中选中的文件的索引
sp.Play();
}
//下一曲
private void button3_Click(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex; //获取当前选中歌曲的索引
//if(index != listBox1.Items.Count-1)
//{
// index++; //下一曲时索引自增
//}
//else if(index==listBox1.Items.Count-1) //当前选中歌曲索引为最后一首歌时,重新回到第一曲
//{
// index = 0;
//}
index++;
if(index == listBox1.Items.Count)
{
index = 0;
}
sp.SoundLocation = Music[index];
listBox1.SelectedIndex = index;
sp.Play();
}
//上一曲
private void button2_Click(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex; //获取当前选中歌曲的索引
//if(index != 0)
//{
// index--; //上一曲时索引自增
//}
//else if (index == 0) //当前选中歌曲索引为最后一首歌时,重新回到第一曲
//{
// index = listBox1.Items.Count-1;
//}
if (index == 0)
{
index = listBox1.Items.Count-1;
}
else
{
index--;
}
sp.SoundLocation = Music[index];
listBox1.SelectedIndex = index;
sp.Play();
}
private void button4_Click(object sender, EventArgs e)
{
//Thread.Sleep();
}
}
}