0

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

4

2 回答 2

2

创建一个自定义类:

public class FileItem
{
    public string FilePath { get; set; }
    public string ShortName { get; set; }
}

然后在从 中获取文件时创建这个类的新实例OpenFileDialog,保存openFileDialog1.FileNametoFilePath属性,然后使用Windows.IO.Path.GetFileName()方法获取短文件名。

ListBox与其直接将文件路径添加到 中,不如添加OpenFileDialog您的类的这个实例。

DisplayMember并将您的属性更改ListBox"ShortName",这样您的文件路径的“短名称”将显示在ListBox.

于 2012-12-16T17:23:04.390 回答
1

您应该可以使用下面的链接来随机播放您的播放列表。

随机播放列表算法

谢谢,海军

于 2012-12-16T18:10:54.527 回答