0
votes

I am displaying list of songs on listbox i have bind media element but i am Unable to get media element name instance in songs.cs file and unable to play song

<ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <MediaElement Name="Player"  Source="{Binding SongUrl}" AutoPlay="False"/>
                                 <Button  Name="Click" Click="Play_Click"  Content="Button"/>
                                <StackPanel Width="150" Height="50">
                                    <TextBlock Text="{Binding SongName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}"  TextAlignment="Center" Foreground="Red"  FontSize="16"/>
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>

example in Songs.cs:

  private void Play_Click(object sender, RoutedEventArgs e)
    {
      Player.play(); (unable to get Media Element name) 
    }
1

1 Answers

2
votes

The media player should not be inside the ItemTemplate since you will be playing only one sound at a time I believe (it would be costly to have a media element for eaach item). So you should move the MEdiaaPlayer outside and on button click do:

private void Play_Click(object sender, RoutedEventArgs e)
{
   Button button=sender as Button;
   Player.Source=((Item)button.DataContext).SongUrl
  Player.play(); (unable to get Media Element name) 
}