Listening for the MediaEnded event in another window
I am making a simple C # WPF video player.
I have 2 Windows: MainWindow (parent window) contains a MediaElement to display the video. PlaylistWindow (child window) is a window containing a ListBox that displays all .avi files in the appRoot path.
Currently, when I double click the ListBox item, it plays this video in the MainWindow. I would like to have an auto play feature, so the next item in the list is automatically played when the current video ends.
I would like the PlaylistWindow to listen for the MediaEnded event triggered by the MediaElement in the MainWindow, so I can do some things on the ListBox in the PlaylistWindow.
How can I subscribe to the MediaEnded event from PlaylistWindow?
Edit to add: I ended up using a different approach as shown below. I don't think this is the best way to do it, but it works for me.
public partial class MainWindow : Window
{
PlaylistWindow PLWindow = new PlaylistWindow();
public MainWindow()
{
InitializeComponent();
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
PLWindow.Owner = this;
PLWindow.Show();
}
private void videoWindow_Ended(object sender, EventArgs e)
{
PLWindow.playNext();
}
}
public partial class PlaylistWindow : Window
{
public void playNext()
{
if (playListBox.SelectedIndex < playListBox.Items.Count - 1)
{
playListBox.SelectedIndex = playListBox.SelectedIndex + 1;
}
else { playListBox.SelectedIndex = 0; }
(Owner as MainWindow).playVideo(playListBox.SelectedValue.ToString());
}
}
I'm still open to learning how to listen for the MediaEnded event on the PlaylistWindow, if the sample code can be posted.
source to share