Bug in adding playlist items via code?

Jul 13, 2010 at 1:55 PM

I am trying to add playListItems via code, but what happens is that the last added playlistitem gets replicated the number items in the List<string>. When I click on the PlayList button, the slide up shows the same clip with the heading 'Now Playing' on both. Clicking one or the other doesnt make and difference. I did try this via xaml, but that behavior seems to work just fine. Instead of using PlayList.Add, i even tried explicitly inserting using Playlist.Insert( ) specifying the index manually, but I get the same result.

        void AddPlaylistItem(List<string> mediaSourceUrls)
        {
            PlaylistItem mediaPlaylistItem = new PlaylistItem();

            SmfVideoPlayer.Playlist.Clear();

            foreach (string mediaUrl in mediaSourceUrls)
            {
                mediaPlaylistItem.MediaSource = new Uri(mediaUrl);

                SmfVideoPlayer.Playlist.Add(mediaPlaylistItem);
            }
           
        }

Jul 13, 2010 at 2:00 PM

Not sure why this is the case, but I rewrote the foreach as follows and it seems to work fine:

            foreach (string mediaUrl in mediaSourceUrls)
            {
                //mediaPlaylistItem.MediaSource = new Uri(mediaUrl);

                //SmfVideoPlayer.Playlist.Add(mediaPlaylistItem);


                SmfVideoPlayer.Playlist.Add(new PlaylistItem { MediaSource = new Uri(mediaUrl) });
            }

Coordinator
Jul 13, 2010 at 6:24 PM

spinz2112, in your original code block you only instantiate one playlistitem, then you change the MediaSource of that playlistitem a bunch of times and add the same playlistitem to the playlist repeatedly.  For that block of code to work you would need to move "PlaylistItem mediaPlaylistItem = new PlaylistItem()" inside of the for-loop.