2012-05-07 31 views
0

我正在制作一个小型媒体播放器,将媒体文件添加到作为mediaelement播放列表的列表框中。当我点击列表框中的一个项目时,它开始播放。我想要做的是在当前结束后让媒体元素自动开始播放列表框中的下一首歌曲/视频。自动播放列表框中的下一个项目

以下是我的歌曲添加到列表框中:

 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); 
      } 
     } 

这里就是我可以点击列表框中的项目,并开始播放

 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(); 
    } 
+0

'player'是什么类? – zimdanen

+0

这与mediaelement相同 – spex

回答

0

订阅的MediaEnded事件MediaElement或MediaPlayer,然后从列表框中选取下一个项目。

编辑

如何挑选下一个项目:

  1. 当前项目的索引在ListBox的SelectedIndex属性
  2. 您可以通过添加1至获得下一个项目SelectedIndex并检查索引是否仍在边界内
  3. 将新索引存储在变量中,该变量将保留新索引,直到项目已播放或直接更改SelectedIndex ;这将改变SelectedItem并移动列表框中的选项

下面的代码没有被编译器检查过!

​​
+0

你能举一个例子说明如何做到这一点吗? – spex

+0

问题是什么?挑选下一个项目或订阅活动? –

+0

如何挑选下一个项目 – spex

相关问题