开发者

how to add mp3 songs to the listbox using wpf?

I have Music folder in my solution explorer..then i want to add th开发者_运维问答at 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


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 ();
}


  • 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);
    }

}


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;
        }`


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;
        }

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜