views:

822

answers:

3

I'm learning C# and WPF by building a WMP-type app. The code below runs fine, selecting a movie from the listbox runs it in the media element. The problem I'm having is finding a way to automatically start the next movie after one ends. Thank You.

The xml file that provides a list of movies:

<?xml version="1.0" encoding="ISO-8859-1"?>

Bear c:\movies\Bear.wmv Butterfly c:\movies\Butterfly.wmv Lake c:\movies\Lake.wmv

xaml

<Window x:Class="WpfAppPlaylistTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="425">

<Window.Resources>
    <XmlDataProvider x:Key="myMoviesXML"

                         Source="c:\Movies\media1.xml"
                         XPath="media"
        />
</Window.Resources>

<Grid DataContext="{Binding ElementName=movieList, Path=SelectedItem}">
    <ListBox ItemsSource="{Binding Source={StaticResource myMoviesXML}, XPath=//media//movie}" IsSynchronizedWithCurrentItem="True" 
     Name="movieList" HorizontalAlignment="Right" Width="114" Margin="0,48,12,32">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding XPath=title}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <MediaElement Source="{Binding XPath=filename}" LoadedBehavior="Play" Name="mediaElement1" Margin="12,26,136,12"  />
</Grid>

+1  A: 

Hey,

MediaElement has a MediaEnded event that should fire when this happens. Then you can programmably select the next item in the list, and play that file.

Brian
Thanks Brian, I'm currently using MediaEnded to stop a timer, but not sure how use it to move to the next movie. I'm thinking some kind of loop in conjunction with IEnumerable or Observablecollection or xmlserializer. I don't know which one is best or maybe one is just a dead end. Advice and/or sample code would be greatly appreciated.
Ken S
Hey, if the mediaended event fires, can't you programmbly select the next entry in the listbox? Do you have the listbox selected value mapped to the mediaelement? So I think changing the selected index fires the listbox chang event, which you can use that to change the media based on the selected value... is that how you have it setup though?
Brian
Yes, the selected listbox value is the source for the mediaelement and it runs fine with a click. To automate this "click" I'm thinking I need a way to index the xml file with an integer to keep track of what playing. How to do this is what I'm currently researching.
Ken S
Hey, if you set SelectedIndex property, doesn't it fire the event just like what would happen if you click it. OK, so if it doesn't, then you could create a method that you pass an index into, and have this code and the listbox click call this method with the index.
Brian
A: 

Thanks Brian, I'm currently using MediaEnded to stop a timer, but not sure how use it to move to the next movie. I'm thinking some kind of loop in conjunction with IEnumerable or Observablecollection or xmlserializer. I don't know which one is best or maybe one is just a dead end. Advice and/or sample code would be greatly appreciated.

Ken S
A: 

Brian's SelectedIndex idea worked perfectly Thanks Brian

ken s