メディア要素のプレイリストとして機能するリストボックスにメディアファイルを追加する小さなメディアプレーヤーを作成しています。リストボックス内のアイテムをクリックすると、再生が開始されます。私がやりたいことは、メディア要素が現在の終了後にリストボックス内の次の曲/ビデオの再生を自動的に開始するようにすることです。
リストボックスに曲を追加する方法は次のとおりです。
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();
}