How can I open a playlist file using c# wmp

1.1k views Asked by At

When I use Player.playlistCollection.newPlaylist("name") it creates a name.wpl file in the default playlist directory (C:\Users\username\Music\Playlists). How can I open this file when I restart the application and pass it to the Player?

3

There are 3 answers

0
botiapa On BEST ANSWER

I solved it by creating custom playlist files where every line is a media file url. So when the application starts, it reads the file line by line and adds it to the wmp playlist.

    private WMPLib.IWMPPlaylist openPlaylist(string playlistName)
    {
        WMPLib.IWMPPlaylist tempPlaylist = Player.newPlaylist(playlistName, null);
        using (System.IO.StreamReader sr = new System.IO.StreamReader(System.IO.Directory.GetCurrentDirectory() + "\\playlists\\" + playlistName + ".cpt"))
        {
            while (sr.Peek() >= 0)
            {
                string tempMediaUrl = sr.ReadLine();
                WMPLib.IWMPMedia tempMedia = Player.newMedia(tempMediaUrl);
                tempPlaylist.appendItem(tempMedia);
            }
            return tempPlaylist;
        }
    }
2
Sv Sv On

Create some .ini settings file in your app directory. Save the path to your playlist in there. Then, when a program starts, read your settings file and get the path to your playlist.

0
SH7 On

Here is an Example. Here i'm getting file from "Video" Folder within the Application Folder and Creating a Playlist and Playing in loop

private void GetMediaFiles()
    {            
        FilePath = Application.StartupPath + "\\Videos\\";
        FileCount = Directory.GetFiles(FilePath).Length;
        Files = Directory.GetFiles(FilePath);

        playlist = axWMPlayer.playlistCollection.newPlaylist("PlaylistName");


        for (int Count = 0; Count < FileCount; Count++)
        {
            media = axWMPlayer.newMedia(Files[Count]);
            playlist.appendItem(media);
        }

        RunMedia();
    }

    private void RunMedia()
    {
        try 
        {
            if (playlist.count > 0)
            {
                axWMPlayer.BringToFront();
                axWMPlayer.currentPlaylist = playlist;
                axWMPlayer.Ctlcontrols.play();
                axWMPlayer.stretchToFit = true;                
            }
            else
            {
                pbDefaultImage.BringToFront();                    
            }
        }

        catch (Exception ex)
        {
            LogException(ex);                
        }
    }