public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] fileList = new string[10000]; // 定义播放列表数的最大值
int numOfMusic; //选中的媒体文件的索引号
int selectOne;//选中的音乐文件
bool playOne = false; // 控制是否循环播放
public void AddFile(string path)
{
if (numOfMusic < 10000)
{
numOfMusic += 1;
fileList[numOfMusic] = path;
}
else
{
MessageBox.Show("不能添加文件!", "播放列表已满");
}
}
private void Form1_Load(object sender, EventArgs e)
{
lstFileList.Items.CopyTo(fileList, 0); //将列表框(lstFileList)中的列表项全部复制到数组(fileList)中
numOfMusic = 0; // 选中第一个媒体文件
CloseBtn();
}
//添加歌曲
public void AddFiles(string path, ListBox lstFiles)
{
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileInfo f in dir.GetFiles("*.mp3"))
{
AddFile(f.FullName);
int i;
string strFile = Convert.ToString(numOfMusic);
for (i = 1; i <= 5 - strFile.Length; i++)
{
strFile += "";
}
strFile = f.Name;
lstFileList.Items.Add(strFile);
}
foreach (DirectoryInfo d in dir.GetDirectories())
{
AddFiles(d.FullName, lstFileList);
}
}
//删除歌曲
public void DelFile(int selectNum)
{
int i;
for (i = selectNum; i <= numOfMusic - 1; i++)
{
fileList[i] = fileList[i + 1];
}
numOfMusic -= 1;
}
public void CloseBtn()
{
btnPlay.Enabled = false;
btnBack.Enabled = false;
btnForward.Enabled = false;
btnStop.Enabled = false;
btnReplay.Enabled = false;
btnDelete.Enabled = false;
}
public void Play(int selectNum)
{
mediaPlayer.URL = fileList[selectNum]; //播放选中的媒体文件
this.Text = "正在播放-- " + lstFileList.SelectedItem.ToString();
}
public void OpenBtn()
{
btnPlay.Enabled = true;
btnBack.Enabled = true;
btnForward.Enabled = true;
}
private void button8_Click(object sender, EventArgs e)
{
mediaPlayer.URL = "";
this.Text = "媒体播放器";
tmrMedia.Enabled = false;
btnReplay.Enabled = false;
lstFileList.SelectedIndex = selectOne - 1;
}
private void lstFileList_Click(object sender, EventArgs e)
{
int i;
odlgMedia.FileName = "";
// 设置默认文件名
odlgMedia.InitialDirectory = "C:\\";
// 设置默认路径
odlgMedia.Filter = "mp3文件|*.mp3|所有文件|*.*";
//设置文件类型
if (odlgMedia.ShowDialog() == DialogResult.OK)
{
string path = odlgMedia.FileName;
FileInfo f = new FileInfo(path);
AddFile(f.FullName);
string strFile = Convert.ToString(numOfMusic);
for (i = 1; i <= 5 - strFile.Length; i++)
{
strFile += "";
}
strFile = f.Name;
lstFileList.Items.Add(strFile);
if (lstFileList.Items.Count > 0)
{
OpenBtn();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
fbdlaMedia.SelectedPath = "c:\\";
fbdlaMedia.ShowNewFolderButton = true;
fbdlaMedia.Description = "请选择媒体文件目录:";
fbdlaMedia.ShowNewFolderButton = false;
if (fbdlaMedia.ShowDialog() == DialogResult.OK)
{
AddFiles(fbdlaMedia.SelectedPath, lstFileList);
if (lstFileList.Items.Count > 0)
{
OpenBtn();
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
int i = lstFileList.SelectedIndex;
if (lstFileList.SelectedIndex >= 0)
{
if ((selectOne == lstFileList.SelectedIndex + 1) && (mediaPlayer.URL != ""))
{
MessageBox.Show("不能删除正在播放的文件", "错误");
}
else
{
DelFile(i + 1);
lstFileList.Items.RemoveAt(i);
if (i < lstFileList.Items.Count)
{
lstFileList.SelectedIndex = i;
}
else if (lstFileList.Items.Count == 0)
{
CloseBtn();
}
else
{
lstFileList.SelectedIndex = 0;
}
}
}
}
private void btnPlay_Click(object sender, EventArgs e)
{
if (lstFileList.SelectedIndex < 0)
{
selectOne = 1;
lstFileList.SelectedIndex = 0;
}
else
{
selectOne = lstFileList.SelectedIndex + 1;
}
Play(selectOne);
tmrMedia.Enabled = true;
btnStop.Enabled = true;
btnReplay.Enabled = true;
}
private void lstFileList_SelectedIndexChanged(object sender, EventArgs e)
{
btnDelete.Enabled = true;
}
private void btnBack_Click(object sender, EventArgs e)
{
if (lstFileList.SelectedIndex > 0)
{
lstFileList.SelectedIndex -= 1;
}
else if (lstFileList.SelectedIndex == 0)
{
lstFileList.SelectedIndex = lstFileList.Items.Count - 1;
}
else
{
lstFileList.SelectedIndex = numOfMusic - 1;
}
selectOne = lstFileList.SelectedIndex + 1;
Play(selectOne);
btnStop.Enabled = true;
btnReplay.Enabled = true;
}
private void button6_Click(object sender, EventArgs e)
{
if (lstFileList.SelectedIndex < lstFileList.Items.Count - 1)
{
lstFileList.SelectedIndex = lstFileList.SelectedIndex + 1;
}
else
{
if (lstFileList.SelectedIndex > 0)
{
lstFileList.SelectedIndex = 0;
}
}
selectOne = lstFileList.SelectedIndex + 1;
Play(selectOne);
btnStop.Enabled = true;
btnReplay.Enabled = true;
}
private void btnReplay_Click(object sender, EventArgs e)
{
if (playOne == true)
{
playOne = false;
btnReplay.FlatStyle = FlatStyle.Standard;
// 设置按钮外观为三维
btnReplay.Text = "单曲循环";
}
else
{
playOne = true;
btnReplay.FlatStyle = FlatStyle.Popup;
// 设置按钮外观为平面显示
btnReplay.Text = "取消循环";
}
lstFileList.SelectedIndex = selectOne - 1;
}
private void lstFileList_DoubleClick(object sender, EventArgs e)
{
//双击播放列表中的媒体文件时,则播放该文件
btnPlay_Click(sender, e);
playOne = false;
btnReplay.Text = "单曲循环";
}
}
C#视频播放器
最新推荐文章于 2024-05-24 14:48:37 发布