Search code examples
c#media-playershuffle

Adding a shuffle to a WMPlib playlist


I'm having trouble adding a shuffle to the code below. Could someone please help me? All I need is it to shuffle the items in playlist randomly after they've been added.

WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(
    @"C:\Users\Callum\Music\ItunesMusic\");

System.IO.FileInfo[] files = dir.GetFiles();

WMPLib.IWMPPlaylist playlist = wplayer.playlistCollection.newPlaylist("myplaylist");

foreach (System.IO.FileInfo file in files)
{
    WMPLib.IWMPMedia media;
    media = wplayer.newMedia(file.FullName);
    playlist.appendItem(media);
}

wplayer.currentPlaylist = playlist;
wplayer.controls.play();

Solution

  • You need to tell the player to shuffle before you play your playlist:

    wplayer.currentPlaylist = playlist;
    wplayer.settings.setMode("shuffle", true); // this does the trick
    wplayer.controls.play();