简体   繁体   English

自动播放列表框中的下一项

[英]Automatically play next item in listbox

I'm making a small media player where I add mediafiles to a listbox which works as a playlist for the mediaelement. 我正在制作一个小型媒体播放器,在其中将媒体文件添加到用作媒体元素的播放列表的列表框中。 When I click on an item in the listbox it starts to play. 当我单击列表框中的项目时,它开始播放。 What I would like to do is to make the mediaelement automatically start playing the next song/video in the listbox after the current has ended. 我想做的是使调解元素在当前播放结束后自动开始播放列表框中的下一首歌曲/视频。

Here's how I add songs to the listbox: 这是我将歌曲添加到列表框中的方法:

        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Multiselect = true;

        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            foreach (string file in ofd.FileNames)   
            {
                FileInfo fileName = new FileInfo(file);                   
                listBox.Items.Add(fileName);
            }
        }

and here's how I can click on an item in the listbox and it starts to play 这就是我如何单击列表框中的项目并开始播放

        private void Button_Click(object sender, RoutedEventArgs e)
    {
        System.Windows.Controls.Button prevButton = player.Tag as System.Windows.Controls.Button;
        System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
        FileInfo fileInfo = button.DataContext as FileInfo;

        // If a file is playing, stop it

        if (prevButton != null)
        {
            player.Tag = null;
            player.Stop();
            prevButton.Background = Brushes.White;

            // if the one thats playing is the one that was clicked -> don't play it

            if (prevButton == button)
                return;
        }

        // Play the one that was clicked

        player.Tag = button;
        player.Source = new Uri(fileInfo.FullName);
        player.Play();
    }

Subscribe to the MediaEnded event of the MediaElement or MediaPlayer and then pick the next item from the listbox. 订阅MediaElement或MediaPlayer的MediaEnded事件,然后从列表框中选择下一项。

EDIT 编辑

How to pick the next item: 如何挑选下一项:

  1. The current item's index is in the SelectedIndex property of the ListBox 当前项目的索引位于ListBox的SelectedIndex属性中
  2. You can get the next item by adding 1 to the SelectedIndex and checking if the index is still within bounds 您可以通过向SelectedIndex加1并检查索引是否仍在范围内来获得下一项
  3. Store the new index in a variable that will keep the new index until the item has been played or change the SelectedIndex directly; 将新索引存储在一个变量中,该变量将保留新索引,直到播放该项目或直接更改SelectedIndex为止; this will change the SelectedItem and move the selection in the ListBox 这将更改SelectedItem并在列表框中移动选择

The code below hasn't been checked by a compiler! 下面的代码尚未由编译器检查!

private int currentSongIndex = -1;

void Player_MediaEnded(object sender, EventArgs e)
{
    if(currentSongIndex == -1)
    {
        currentSongIndex = listBox.SelectedIndex;
    }
    currentSongIndex++;
    if(currentSongIndex < listBox.Items.Count)
    {
        player.Play(listBox.Items[currentSongIndex]);
    }
    else
    {
        // last song in listbox has been played
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM