我正在尝试制作简单的mp3播放器。当我使用此
时 listBox2.Items.Add(openFileDialog1.FileName);
将歌曲添加到我的列表框中它正在工作,但它显示文件目录,所以我改变了这样
listBox2.Items.Add(openFileDialog1.SafeFileName);
然后它看起来在listbox1上的歌曲名称但是当我点击播放按钮时它不起作用:(
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication12222
{
public partial class Form1 : Form
{
[DllImport("winmm.dll")]
private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
public string Pcommand;
public bool isOpen;
public Form1()
{
InitializeComponent();
}
public void Stop()
{
Pcommand = "close MediaFile";
mciSendString(Pcommand, null, 0, IntPtr.Zero);
isOpen = false;
}
public void Start()
{
Pcommand = "open \"" + listBox1.SelectedItem + "\" type mpegvideo alias MediaFile";
mciSendString(Pcommand, null, 0, IntPtr.Zero);
isOpen = true;
Play(true);
}
private void button10_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Media File(*.mpg,*.dat,*.avi,*.wmv,*.wav,*.mp3)|*.wav;*.mp3;*.mpg;*.dat;*.avi;*.wmv";
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName != ""){
// listBox1.Items.Add(openFileDialog1.SafeFileName);
listBox2.Items.Add(openFileDialog1.FileName);
}
}
private void button1_Click(object sender, EventArgs e)
{
Start();
}
private void button4_Click(object sender, EventArgs e)
{
Stop();
}
public void Play(bool loop)
{
if (isOpen)
{
Pcommand = "play MediaFile";
if (loop)
Pcommand += " REPEAT";
mciSendString(Pcommand, null, 0, IntPtr.Zero);
}
}
int x;
private void button2_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex ==listBox1.Items.Count-1 ) { x++; }
else
{
Stop();
listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
Start();
}
}
int y;
private void button5_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex ==0) { y++; }
else
{
Stop();
listBox1.SelectedIndex = listBox1.SelectedIndex - 1;
Start();
}
}
private void button3_Click(object sender, EventArgs e)
{
listBox1.SelectedIndex = 0;
Stop();
Start();
}
private void button6_Click(object sender, EventArgs e)
{
listBox1.SelectedIndex = listBox1.Items.Count - 1;
Stop();
Start();
}
private void button9_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
Pcommand = "close MediaFile";
mciSendString(Pcommand, null, 0, IntPtr.Zero);
isOpen = false;
}
private void button8_Click(object sender, EventArgs e)
{
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
}
}
额外的问题是可以混合(随机)列表框。我在mp3播放器上添加歌曲然后当我想点击混合按钮我希望列表混合。是否有任何listbox命令?
答案 0 :(得分:2)
创建自定义类:
public class FileItem
{
public string FilePath { get; set; }
public string ShortName { get; set; }
}
然后在从OpenFileDialog
获取文件时创建此类的新实例,将openFileDialog1.FileName
保存到FilePath
属性,然后使用{获取短文件名{1}}方法。
不是直接从Windows.IO.Path.GetFileName()
向ListBox
添加文件路径,而是添加此类的实例。
并将OpenFileDialog
的{{1}}属性更改为DisplayMember
,这样文件路径的“短名称”就会显示在ListBox
中。
答案 1 :(得分:1)