c#WMPLib随机播放列表框中的MP3文件

时间:2017-04-12 19:30:21

标签: c# winforms

我正在使用WMPLib在c#winforms中构建一个MP3播放器。我有代码,允许用户将MP3文件上传到listbox1。我希望用户能够随机播放listbox1中的项目,以便播放随机项目(MP3)。目前,我有一些代码可以对listbox1中的项进行随机播放,但是初始索引中的项目会播放。我希望这是有道理的。

以下是我的用户上传文件的代码。

 private void uploadSongs()
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Multiselect = true;

        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            files = openFileDialog1.SafeFileNames;
            paths = openFileDialog1.FileNames;
            for (int i = 0; i < files.Length; i++)
            {
                filelist.Items.Add(files[i]);

            }

        }

    }

以下是对listbox1项目进行随机播放的代码:

   private void shuffleBttn_Click(object sender, EventArgs e)
    {

        ListBox.ObjectCollection list = fielist.Items;
        Random random = new Random();
        int w = list.Count;
        filelist.BeginUpdate();

        while (w > 1)
        {
            w--;
            int u = random.Next(w + 1);
            object value = list[u];
            list[u] = list[w];
            list[w] = value;
        }

        filelist.EndUpdate();
        filelist.Invalidate();

    }

我已将以下播放列表代码添加到shuffleBttn_Click

WMPLib.IWMPPlaylist playlist = WMPPlayer.playlistCollection.newPlaylist("myplaylist");
        WMPLib.IWMPMedia media;
        foreach (object item in filelist.Items)
        {
            media = WMPPlayer.newMedia((string)item);
            playlist.appendItem(media);
        }
        WMPPlayer.currentPlaylist = playlist;
        WMPPlayer.Ctlcontrols.play();

1 个答案:

答案 0 :(得分:0)

在VS调试器中进入代码,你会发现listBox1.SelectedIndex的值在while循环中没有变化。因此,您最终会使用列表中的相同项目多次设置WMPPlayer.URL

考虑创建播放列表。

...
//remove these 2 lines that cause the problem
//value = listBox1.SelectedIndex;
//WMPPlayer.URL = filepaths[listBox1.SelectedIndex];
...
listBox1.EndUpdate();
listBox1.Invalidate();

//after shuffling the items in listbox, create a playlist from the listbox

WMPLib.IWMPPlaylist playlist = WMPPlayer.playlistCollection.newPlaylist("myplaylist");
WMPLib.IWMPMedia media;
foreach (object item in listBox1.Items)
{
    media = WMPPlayer.newMedia((string)item);
    playlist.appendItem(media);
}    
WMPPlayer.currentPlaylist = playlist;
WMPPlayer.Ctlcontrols.play();

编辑:适用于我的设备的完整代码

    private void uploadSongs(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Multiselect = true;

        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            var files = openFileDialog1.FileNames; //I also changed this line
            for (int i = 0; i < files.Length; i++)
            {
                filelist.Items.Add(files[i]);
            }
        }
    }

    private void shuffleBttn_Click(object sender, EventArgs e)
    {
        ListBox.ObjectCollection list = filelist.Items;
        Random random = new Random();
        int w = list.Count;
        filelist.BeginUpdate();

        while (w > 1)
        {
            w--;
            int u = random.Next(w + 1);
            object value = list[u];
            list[u] = list[w];
            list[w] = value;
        }

        filelist.EndUpdate();
        filelist.Invalidate();

        WMPLib.IWMPPlaylist playlist = WMPPlayer.playlistCollection.newPlaylist("myplaylist");
        WMPLib.IWMPMedia media;
        foreach (object item in filelist.Items)
        {
            media = WMPPlayer.newMedia((string)item);
            playlist.appendItem(media);
        }
        WMPPlayer.currentPlaylist = playlist;
        WMPPlayer.Ctlcontrols.play();
     }