You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

  1. IsShuffle never gets updated: You initialized it as false but never toggle it when the checkbox is checked/unchecked. So the shuffle logic is never actually triggered.
  2. Stale MediaFile reference: 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.
  3. 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).
  4. 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

  1. Persistent Random Instance: By declaring _rand as a class-level variable, we avoid duplicate random numbers caused by creating new instances too quickly.
  2. Shuffle State Toggle: The checkBox2_CheckedChanged event now updates IsShuffle whenever the checkbox is toggled, so the logic knows when to use random play.
  3. PlayState Trigger: Instead of running the shuffle/next logic in the checkbox code, we hook it to the media player's PlayStateChange event. This ensures the next song (random or sequential) plays only when the current one finishes—the expected behavior for media players.
  4. Cleaned-Up Playback Flow: By relying on the SelectedIndexChanged event 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:33:06