-1

I have Music folder in my solution explorer..then i want to add that songs to the list box control after that i want to play the selected songs from listbox in the media element using wpf?
Please Help me. Thanks

Radhika
  • 1
  • 3

4 Answers4

1

To make play behaviour eexplici on a button click , refer this:

Xaml :

<MediaElement x:Name="media" Source="{Binding   
             ElementName=listbox,Path=SelectedItem}" 
             LoadedBehavior="Manual" UnloadedBehavior="Manual"/> 
 <Button Click="Button_Click" Height="27" VerticalAlignment="Bottom"   
         HorizontalAlignment="Left" Width="62">Play</Button>

Code Behind :-

private void Button_Click (object sender, RoutedEventArgs e) {
       media.Play ();
}
Shraddha
  • 11
  • 1
0
  • You should implement business logic to browse the directory you are targetting. Prepare a collection of Items. Bind these to Listbox
  • For playing the song, bind selected item to MediaElement.

I will try to compile some simple solution and update if you still need further help.

Updating simple solution:

Xaml:

<StackPanel Orientation="Vertical">
    <ListBox ItemsSource="{Binding}" x:Name="fileList"></ListBox>
    <MediaElement x:Name="mediaElement" Source="{Binding ElementName=fileList, Path=SelectedItem}"/>
</StackPanel>

Code Behind:

public partial class Window1 : Window {
    ObservableCollection<string> mFileList;

    public Window1 () {
        InitializeComponent ();
        GetFiles(@"..\songs");

        this.DataContext = mFileList;

    }

    private void GetFiles (string folderPath) {
       string[] files = Directory.GetFiles(folderPath);
       mFileList = new ObservableCollection<string> (files);
    }

}
Ujjwal
  • 386
  • 3
  • 18
  • thank you for giving an idea..i need code snippet.please help me. – Radhika Aug 26 '11 at 06:26
  • I have a reqiurement, I need to play multiple audio songs on window using wpf.Can anyone suggest the best way to implement this. I have window. There I will display the list of the songs in listbox.. User should able to select the multiple songs from the listbox and click play. I have to play the all audio songs selected by user one by one. I will appreciate your help. – Radhika Aug 29 '11 at 05:11
  • As far as I know, Media Element doesn't support inbuilt listing. But no reason to worry. You can maintain your own collection or list of the selected items. MediaElement will fire 'MediaEnded' event when it is done with playing current song. Just capture this event and play next song on this till you have song available in the collection – Ujjwal Aug 29 '11 at 06:28
0
You need to handle the mediaended event as below :-

<MediaElement x:Name="media" Source="{Binding ElementName=listbox,Path=SelectedItem}"   MediaEnded="media_MediaEnded"
                 ></MediaElement>

Codebehind :-
` private void media_MediaEnded (object sender, RoutedEventArgs e) {
        if (listbox.SelectedIndex < listbox.Items.Count - 1) {
            listbox.SelectedIndex = listbox.SelectedIndex + 1;
        }`
Shraddha
  • 11
  • 1
0
You need to handle the mediaended event as below :-     
<MediaElement x:Name="media" Source="{Binding ElementName=listbox,Path=SelectedItem}" Margin="0,119,78,64"  MediaEnded="media_MediaEnded"
                 ></MediaElement>

   private void media_MediaEnded (object sender, RoutedEventArgs e) {
        if (listbox.SelectedIndex < listbox.Items.Count - 1) {
            listbox.SelectedIndex = listbox.SelectedIndex + 1;
        }

    }
Shraddha
  • 11
  • 1