none
MediaPlayer shuffle is not random RRS feed

  • Question

  • I used the MediaPlayer library in my code to play a list of musics. I want the sequence to be random. However, the shuffle function did not give me any randomness. Here is my code snippet:

     public partial class FrmGameOption : Form
        {

           public WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();

            wplayer.controls.stop();
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(
                @"music\map\");

                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.settings.setMode("shuffle", true);
                wplayer.settings.setMode("Loop", true);
                wplayer.controls.play();

    }

     
    Friday, April 3, 2020 7:18 PM

Answers

  • Maybe execute ‘wplayer.controls.next()’ before ‘wplayer.controls.play()’.

    In order to shuffle the files before creating the playlist, try a known method:

    var r = new Random();

    FileInfo[] files = dir.GetFiles().OrderBy( f => r.Next() ).ToArray();

    • Marked as answer by antidinosaur Monday, April 6, 2020 10:09 PM
    Sunday, April 5, 2020 6:26 AM

All replies

  • I tested with a few songs, and the first one is always the same played in the list, but others are random (on Windows 10, WMP 12)
    • Edited by Castorix31 Saturday, April 4, 2020 6:33 AM
    Saturday, April 4, 2020 6:26 AM
  • Thank you very much. Because the first music is long, I did not check the rest of them. But still, is it any way I can make the first music different every time? Say, whether I can arrange the sequence in a "foreach" comment?
    Sunday, April 5, 2020 2:53 AM
  • Maybe execute ‘wplayer.controls.next()’ before ‘wplayer.controls.play()’.

    In order to shuffle the files before creating the playlist, try a known method:

    var r = new Random();

    FileInfo[] files = dir.GetFiles().OrderBy( f => r.Next() ).ToArray();

    • Marked as answer by antidinosaur Monday, April 6, 2020 10:09 PM
    Sunday, April 5, 2020 6:26 AM