tags:

views:

755

answers:

1

Hi 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  A: 

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

Vimvq1987
Hi Vimvq can you post a code snippet about that?Sorry i'm a new one in C# ,Thanks Vimvq
JayJay
Hi I put different playlist because them need to be played in different time with a Timer that call the playlist in right time.:)
JayJay
I just added demo code. A quick (and maybe dirty) code. Now I have to go to bed :). Glad that it can help
Vimvq1987
Thanks so much for your help , i can change the code and so i can select the song that i like but how when the Window Media PLayer change song the listbox did not follow the song but keep in stuck.,.How i can syncronize both???
JayJay
You might want to look into the events of WMP, if you don't know what events are then you should look up the C# documentation on them. The event you looking for is CurrentItemChange I think, see also at the bottom of http://msdn.microsoft.com/en-us/library/bb248435(VS.85).aspx
TomWij
Hi TomWij i tried that features but still cannot work well ,however i-m looking around to work out this feature.Thanks TomWij
JayJay