WMP library in C# - playlist from a list

2.8k Views Asked by At

I just found out about the Windows Media Player library for C#, since I am making a stupid little app for my friend I thought that I would add it in (since he loves music).

string ply = "Playlist";
        WMPLib.IWMPPlaylist pls;
        WMPLib.IWMPPlaylistArray plItems;

        plItems = axWindowsMediaPlayer1.playlistCollection.getByName(ply);

        if (plItems.count == 0)
        {
            pls = axWindowsMediaPlayer1.playlistCollection.newPlaylist(ply);
        }
        else
        {

            plItems.Item(0);

            string line;
            System.IO.StreamReader stream = new StreamReader(pth);
            while ((line = stream.ReadLine()) != null)
            {
                if (File.Exists(line))
                {
                    WMPLib.IWMPMedia m1 = axWindowsMediaPlayer1.newMedia(line);

                    pls.appendItem(m1); /*gives an error here (use of unassigned variable)*/
                }
            }
        }
    }

I've tried creating an instance of the playlist, but I can't get it to allow that line, any help would be appreciated (Note that the 'pth' string was declare earlier in the method

1

There are 1 best solutions below

0
On

The problem is that if plItems.count is greater than 0, then pls is never set to anything, so you can't call appendItem on it.

Perhaps you meant to say

pls = plItems.Item(0);

instead of

plItems.Item(0);

(which doesn't do anything) in your else branch. This will cause your app to add media items to the first playlist in the library.