我正在尝试制作简单的 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 播放器上添加歌曲然后当我想单击混合按钮时我想要混合列表。是否有任何列表框命令?