【问题标题】:Shuffle Function with Checkbox带复选框的随机播放功能
【发布时间】:2015-06-24 10:43:47
【问题描述】:

我需要一个复选框来激活或停用播放列表(数据表)中曲目的随机播放。如果选中该复选框并且播放曲目结束,则下一个随机曲目应自动开始播放。

这是我在曲目结束时自动调用的事件:

// This event is automatically called when a track has ended
private void mePlayer_MediaEnded(object sender, RoutedEventArgs e)
{
    // Check, if the the (last calculated) Index is in the datagrid range
    // -1 is used, cause the indexes starts by 0 not 1
    if (newIndex >= (dgPlayList.Items.Count - 1)) 
    {
        // If the end of the datagrid is reached, set index to 0 for playing the first track
        newIndex = 0;
    }
    else
    {
        // Is there a following track in the datagrid, then use the next calculated index
        newIndex += 1;
    }
    Console.WriteLine("Set index to: " + newIndex);

    // Tell the media player to use the calculated nexIndex
    mePlayer.Source = new Uri(playList.Rows[newIndex]["Dateiname"].ToString());
    lblFileName.Content = getFileName(playList.Rows[newIndex]["Dateiname"].ToString());

    // Starts to play and set it to play
    mePlayer.Play();
    isPlaying = true;        
}

我对 C# 很陌生,只是不知道如何集成播放器在曲目结束后播放随机曲目。

【问题讨论】:

    标签: c# .net checkbox shuffle


    【解决方案1】:

    您可以使用Random 类来创建一个随机值。

    define a Random variable outside the function.

    Random r = new Random();
    

    并像这样使用它

    if (cb_Shuffle.IsChecked == true)
    {
        newIndex = r.Next(dgPlayList.Items.Count);
    }
    else
    {
        if (newIndex >= (dgPlayList.Items.Count - 1))
        {
            // If the end of the datagrid is reached, set index to 0 for playing the first track
            newIndex = 0;
        }
        else
        {
            // Is there a following track in the datagrid, then use the next calculated index
            newIndex += 1;
        }
    }
    

    那么newIndex 将是一个小于dgPlayList.Items.Count 的非负数

    你应该check if the checkbox is checked or not来决定选择随机数还是下一首

    【讨论】:

    • 谢谢!如何在上述事件中将newIndex = r.Next(dgPlayList.Items.Count); 与复选框集成?我在表单中创建了一个复选框,然后尝试添加类似 if (checkbox_shuffle.checked) ... 的内容 - 但我总是收到错误 Identifier expected; "checked" is a keyword".. 我想仅在选中复选框时使用随机数设置 newIndex。跨度>
    • 应该是Checked 而不是checked
    • 它是if (cb_Shuffle.IsChecked == true),它就像魅力一样!谢谢你的帮助! :)
    猜你喜欢
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 2014-09-09
    • 2023-01-23
    相关资源
    最近更新 更多