WMP无法基于ListBox选中项实现随机播放问题求助
Fixing the Shuffle CheckBox Issue in Your Media Player
Let's break down why your shuffle checkbox isn't working and fix it step by step:
Core Issues in Your Current Code
IsShufflenever gets updated: You initialized it asfalsebut never toggle it when the checkbox is checked/unchecked. So the shuffle logic is never actually triggered.- Stale
MediaFilereference: You grab the selected file before changing the listbox index, so even when you pick a random index, you're still playing the old file. - Random number duplication: Creating a new
Random()instance every time can generate the same number if the code runs quickly (since it uses system time as seed). - Redundant play calls: Your listbox change event calls
play()twice, which is unnecessary.
Fixed Code
First, update your class-level variables to hold a persistent random instance and shuffle state:
private Random _rand = new Random(); private bool IsShuffle = false;
Updated ListBox SelectedIndexChanged Event
This cleans up redundant play calls and ensures we set the URL before starting playback:
private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e) { MediaFile file = listBox1.SelectedItem as MediaFile; if (file != null) { wmp.URL = file.Path; wmp.Ctlcontrols.play(); // Removed duplicate blumemediaplayer call since you're using wmp for playback } }
Fixed CheckBox & Playback Logic
We'll split this into two parts: toggling the shuffle state, and triggering the next song when the current one finishes (the proper place for this logic):
// Toggle shuffle state when checkbox is checked/unchecked private void checkBox2_CheckedChanged(object sender, EventArgs e) { IsShuffle = checkBox2.Checked; } // Hook this to your WMP control's PlayStateChange event private void wmp_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e) { // Only run when the current media has finished or stopped if (e.newState == (int)WMPPlayState.wmppsStopped || e.newState == (int)WMPPlayState.wmppsMediaEnded) { if (listBox1.Items.Count == 0) return; // Exit if no files are loaded if (IsShuffle) { // Generate a valid random index (covers all items in the list) int randomIndex = _rand.Next(0, listBox1.Items.Count); listBox1.SelectedIndex = randomIndex; } else { // Move to next item, loop back to start if at the end of the list int nextIndex = listBox1.SelectedIndex + 1; listBox1.SelectedIndex = nextIndex >= listBox1.Items.Count ? 0 : nextIndex; } } }
Key Fixes Explained
- Persistent Random Instance: By declaring
_randas a class-level variable, we avoid duplicate random numbers caused by creating new instances too quickly. - Shuffle State Toggle: The
checkBox2_CheckedChangedevent now updatesIsShufflewhenever the checkbox is toggled, so the logic knows when to use random play. - PlayState Trigger: Instead of running the shuffle/next logic in the checkbox code, we hook it to the media player's
PlayStateChangeevent. This ensures the next song (random or sequential) plays only when the current one finishes—the expected behavior for media players. - Cleaned-Up Playback Flow: By relying on the
SelectedIndexChangedevent to set the URL and start playback, we eliminate stale file references and redundant code.
Don't forget to hook up the wmp_PlayStateChange event to your Windows Media Player control (you can do this in the designer or your form's constructor).
内容的提问来源于stack exchange,提问作者C1sc0
相关产品推荐
相关产品推荐

