WMP库将多个文件添加到列表框中

时间:2017-03-06 16:02:04

标签: c# winforms listbox wmp

我正在使用wmp库创建一个MP3播放器。我想将多个文件添加到我的歌曲所在的列表框中。但不幸的是,尽管选择了多首歌,但我只添加了一首歌。我找不到我的错误。

        openfile.FileName = String.Empty;
        openfile.InitialDirectory = "D:\\";
        openfile.Filter = "MP3 Files(*.mp3)|*.mp3";
        openfile.RestoreDirectory = true;
        openfile.Multiselect = true;

        if (openfile.ShowDialog() == DialogResult.OK)
        {
            pictureBoxEqualizer.Visible = true;
            listBoxSongs.Items.Clear();
            if (MediaPlayer.currentPlaylist.name != "myplaylist")
            {
                WMPLib.IWMPPlaylist songs = MediaPlayer.playlistCollection.newPlaylist("myplaylist");
                WMPLib.IWMPMedia media;
                if (!playlist.Contains(openfile.SafeFileName))
                {

                        playpath.Add(openfile.FileName);
                        foreach (string file in playpath)
                        {
                            timer.Enabled = true;
                            media = MediaPlayer.newMedia(file);
                            songs.appendItem(media);
                        }
                        MediaPlayer.currentPlaylist = songs;
                        WMPLib.IWMPMedia current = MediaPlayer.currentPlaylist.get_Item(curr);
                        MediaPlayer.controls.playItem(current);

                        playlist.Add(MediaPlayer.currentMedia.getItemInfo("Artist") + " - " + MediaPlayer.currentMedia.name);


                    }
                    {
                        MessageBox.Show("Error!" + ex.Message,
                        "Error message!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("The song is already in the list");
                }  }
            else
            {
                if (!playlist.Contains(openfile.SafeFileName))
                {
                        WMPLib.IWMPPlaylist songs = MediaPlayer.currentPlaylist;
                        WMPLib.IWMPMedia media;
                        playpath.Add(openfile.FileName);
                        media = MediaPlayer.newMedia(openfile.FileName);
                        songs.appendItem(media);
                        MediaPlayer.currentPlaylist = songs;
                        WMPLib.IWMPMedia current = MediaPlayer.currentPlaylist.get_Item(curr);
                        MediaPlayer.controls.playItem(current);

                        playlist.Add(MediaPlayer.currentMedia.getItemInfo("Artist") + " - " + MediaPlayer.currentMedia.name);

                    }
                    {
                        MessageBox.Show("Error!" + ex.Message,
                        "Error message!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("The song is already in the list");
                }
                listBoxSongs.Items.Clear();

                foreach (string item in playlist)
                {
                    listBoxSongs.Items.Add(item); 
                }
                }
                listBoxSongs.SetSelected(curr, true);
                curr++;

        Title();

1 个答案:

答案 0 :(得分:0)

            OpenFileDialog FileDialog = new OpenFileDialog();
            FileDialog.Multiselect = true;
            if (FileDialog.ShowDialog() == DialogResult.OK) ;
            {
                fi = FileDialog.SafeFileNames;
                pt = FileDialog.FileNames;
                for (int i = 0; i < fi.Length; i++)
                {
                    listBox1.Items.Add(fi[i]);
                }
            }