How to Play a Playlist (2 Viewers)

hbe02

Portal Member
April 1, 2008
26
1
Home Country
it has come to my attention that using
Code:
MediaPortal.Player.g_Player.Play(whatever.mp3, MediaPortal.Player.g_Player.MediaType.Music)
will play an mp3 file

and it plays music perfectly, but with one major problem.

when i say put playlist.wpl instead of the .mp3 file above. the playlist plays, but MP plays it as a single file, so the song list does not appear in "current playlist" and i cannot go "next track"

any ideas?
 

and-81

Retired Team Member
  • Premium Supporter
  • March 7, 2005
    2,257
    183
    Melbourne
    Home Country
    Australia Australia
    hmm, I'll look into it further and get back to you ... in the meantime, check out the mediaportal source code and see how it's done there.
     

    jburnette

    Portal Pro
    August 24, 2006
    758
    116
    Kentucky
    Home Country
    United States of America United States of America
    I think that for playing a playlist you have to use the PlaylistPlayer Class in the Mediaportal.Playlists namespace. I've never actually used it, I just remember seeing it.
     

    hbe02

    Portal Member
    April 1, 2008
    26
    1
    Home Country
    I looked around the class, cant find anything that works, i tried:
    MediaPortal.Playlists.PlayListPlayer.SingletonPlayer.g_Player.Play(FileToPlay.Trim());

    but that gave me the exact same result as before

    btw i tried the following:
    went to music folder
    added an album to playlist
    it was playing under current playlist and now playing perfectly and i can click next and that works
    i treid "save" typed a name and pressed "done"
    then i clicked "MY Playlists"
    MP hangs for about 4 seconds then just resumes operation in current screen.. does not go to "My Playlists"
    i tried searching with windows search for the playlist i entered..and i didnt find it.
    any ideas?
     

    darick_c

    Portal Member
    July 10, 2008
    13
    1
    Idaho
    Home Country
    United States of America United States of America
    Here is how I play a playlist:

    Code:
    using MediaPortal.Playlists;
    
    
    
    public void PlayPlaylist(string playlistName)
            {
                PlayListPlayer playlistPlayer = PlayListPlayer.SingletonPlayer;
                playlist = playlistPlayer.GetPlaylist(PlayListType.PLAYLIST_MUSIC);
    
                string strPlayListPath = string.Empty;
                using (MediaPortal.Profile.Settings xmlreader = new MediaPortal.Profile.Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml")))
                {
                    strPlayListPath = xmlreader.GetValueAsString("music", "playlists", string.Empty);
                    strPlayListPath = MediaPortal.Util.Utils.RemoveTrailingSlash(strPlayListPath);
                }
                string playlistFileName = Path.Combine(strPlayListPath, playlistName);
                PlayList tempPlaylist = GetPlaylistByPath(playlistFileName);
    
                if (!File.Exists(playlistFileName) && tempPlaylist != null)
                    return;
    
                playlist.Clear();
                playlist.Name = tempPlaylist.Name;
                foreach (PlayListItem item in tempPlaylist)
                {
                    playlist.Add(item);
                }
    
                if (playlistPlayer.GetPlaylist(PlayListType.PLAYLIST_MUSIC).Count > 0)
                {
                                    playlistPlayer.Reset();
                                    playlistPlayer.CurrentPlaylistType = PlayListType.PLAYLIST_MUSIC;
                                    playlistPlayer.Play(0);
                 }
            }
    
    private PlayList GetPlaylistByPath(string path)
            {
                IPlayListIO io;
                string ext = System.IO.Path.GetExtension(path);
                PlayList playlist = new PlayList();
                switch (ext.ToLower())
                {
                    case ".m3u":
                        io = new PlayListM3uIO();
                        break;
                    case ".pls":
                        io = new PlayListM3uIO();
                        break;
                    case ".b4s":
                        io = new PlayListM3uIO();
                        break;
                    case ".wpl":
                        io = new PlayListM3uIO();
                        break;
                    default:
                        return playlist;
                }
                if (io.Load(playlist, path))
                    return playlist;
                else
                    return null;
            }

    Hope it works for you,
    Darick

    If you are calling the above from a thread other than the main, then you may need to invoke it to run on the main thread, otherwise it could cause an error.

    Code:
    private delegate void PlayPlaylistHandler(string playlistName);
    public void SomeMethod()
    {
        string someplaylist = "someplaylist";
        System.Windows.Forms.Control currentform =     System.Windows.Forms.Form.FromHandle(GUIGraphicsContext.ActiveForm);
    
       if (currentform.InvokeRequired)
       {
            PlayPlaylistHandler handler = new PlayPlaylistHandler(PlayPlaylist);
            currentform.Invoke(handler, new object[] {someplaylist });
       }
       else
       {
            PlayPlaylist(someplaylist);
       }

    Hope this helps,
    Darick
     

    Fjoms

    Portal Member
    January 13, 2008
    8
    2
    Home Country
    Norway Norway
    I have just been struggling with the same thing. I tried your code, Darick, but MP only plays the first file in the m3u playlist. When I open "current playlist" it is empty, and when I go to "now playing" there is no "next track" waiting. I want to be able to load an m3u playlist in code, and then skip back and forth between its tracks. Does this work with the code you posted? If so, I must be doing something wrong... ;)
     

    Users who are viewing this thread

    Top Bottom