0
votes

i'm building a Music PLayer and so i choose to use the library of Window Media Player: Now i got stuck 'cos i wish show the song's name in a listBox and change songs in real time but i don't know how go on. I store songs from a Folder and so when the Music Player run the songs from the Url choose. I show you a code snippet :

     private void PlaylistMidday(String folder, string extendsion)
   {
         string myPlaylist = "D:\\Music\\The_Chemical_Brothers-Do_It_Again-(US_CDM)-2007-SAW\\";
        ListView musicList = new ListView();

        WMPLib.IWMPPlaylist pl;

        WMPLib.IWMPPlaylistArray plItems;

        plItems = player1.playlistCollection.getByName(myPlaylist);

        if (plItems.count == 0)

            pl = player1.playlistCollection.newPlaylist(myPlaylist);

        else

            pl = plItems.Item(0);

        DirectoryInfo dir = new DirectoryInfo(folder);

        FileInfo[] files = dir.GetFiles(extendsion,  SearchOption.AllDirectories);

        foreach (FileInfo file in files)
        {

            string musicFile01 = file.FullName;

            string mName = file.Name;

            ListViewItem item = new ListViewItem(mName);

            musicList.Items.Add(item);

            WMPLib.IWMPMedia m1 = player1.newMedia(musicFile01);

            pl.appendItem(m1);

        }

        player1.currentPlaylist = pl;

        player1.Ctlcontrols.play();

   }

On Load i decide to play the songs of "myPLaylist" so i ask you do you know some way how to show the songs of my playlist in a listbox and when i click on the selected item i will change songs?

Thansk for your support.

Nice Regards

1

1 Answers

1
votes

Instead of adding songs to playlist, you can add them to a List<string> as a return value. On load event, you just call the method that get list of media file paths in the folder, and then add them into a listbox.

To change the song being played, you just need to add SelectedValueChanged/SelectedItemChanged event, and in this event, get the file path that is currently selected in the listbox, then have WMP played it for you :)

 private void Form1_Load(object sender, EventArgs e)
        {

            List<string> str = GetListOfFiles(@"D:\Music\Bee Gees - Their Greatest Hits - The Record");
            listBox1.DataSource = str;
            listBox1.DisplayMember = "str";


        }

        private List<string> GetListOfFiles(string Folder)
        {
            DirectoryInfo dir = new DirectoryInfo(Folder);
            FileInfo[] files = dir.GetFiles("*.mp3", SearchOption.AllDirectories);
            List<string> str = new List<string>();
            foreach (FileInfo file in files)
            {                             
                str.Add(file.FullName);                   

            }
            return str;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strSelected = listBox1.SelectedValue.ToString();
            MessageBox.Show(strSelected); //Just demo, you can add code that have WMP played this file here 
        }

a quick solution. :). Not very good, but it works. Help this hope