如何在列表中选择和播放多首歌曲?

时间:2011-08-29 05:08:57

标签: wpf listbox mediaelement

我有一个要求,我需要使用wpf在窗口上播放多首音频歌曲。任何人都建议实施此方法的最佳方法。

我有窗户。在那里,我将在列表框中显示歌曲列表。用户应该能够从列表框中选择多首歌曲,然后单击播放。

我必须逐一播放用户选择的所有音频歌曲。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

最简单的方法是使ListBox控件的ItemsSource属性绑定到某个集合(例如“SongList”)。

<ListBox Name="lstSongs" ItemsSource="{Binding Path=SongList}" SelectionMode="Extended" Grid.Row="1" />

在页面上有一个MediaElement控件,用于收听下一首歌

<MediaElement Name="player" MediaEnded="MediaElement_MediaEnded" LoadedBehavior="Play" UnloadedBehavior="Stop" Source="{Binding Path=CurrentlyPlaying}" />

当用户单击表单上的按钮时,当前选定的项目将添加到队列中。通过读取ListBox.SelectedItems属性可以找到当前选定的项目。

private void cmdQueueItems_Click(object sender, RoutedEventArgs e)
{
    Queue = lstSongs.SelectedItems.OfType<Uri>().ToList();
    playNext();
}

然后开始播放队列中的第一个项目,当引发MediaElement.MediaEnded事件时,如果有一个项目可用,则将当前正在播放的项目替换为队列中的下一个项目。

private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
    playNext();
}

在用户点击预定义的停止按钮之前,您将执行此操作。

playNext()方法只是

private void playNext()
{
    CurrentlyPlaying = Queue.FirstOrDefault();

    if (CurrentlyPlaying != null)
        Queue.Remove(CurrentlyPlaying);
}

(确保CurrentlyPlaying属性引发INotifyPropertyChanged.PropertyChanged事件)

<强> MainWindow.xaml.cs

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

    public List<Uri> Queue { get; private set; }

    #region CurrentlyPlaying Definition

    private Uri _CurrentlyPlaying = null;

    public Uri CurrentlyPlaying
    {
        get
        {
            return _CurrentlyPlaying;
        }
        set
        {
            _CurrentlyPlaying = value;
            OnPropertyChanged("CurrentlyPlaying");
        }
    }

    #endregion // end of CurrentlyPlaying region

    public System.Collections.ObjectModel.ObservableCollection<Uri> SongList { get; private set; }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;

        SongList = new System.Collections.ObjectModel.ObservableCollection<Uri>();

        SongList.Add(new Uri(@"E:\Music\_relaxation\African Drums - Tribal Music.mp3"));
        SongList.Add(new Uri(@"E:\Music\Disturbed\Disturbed - A Welcme Burden.mp3"));
    }

    private void cmdQueueItems_Click(object sender, RoutedEventArgs e)
    {
        Queue = lstSongs.SelectedItems.OfType<Uri>().ToList();
        playNext();
    }

    private void cmdSkipItem_Click(object sender, RoutedEventArgs e)
    {
        playNext();
    }

    private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
    {
        playNext();
    }

    private void playNext()
    {
        CurrentlyPlaying = Queue.FirstOrDefault();

        if (CurrentlyPlaying != null)
            Queue.Remove(CurrentlyPlaying);
    }
}

<强> MainWindow.xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <MediaElement Name="player" MediaEnded="MediaElement_MediaEnded" LoadedBehavior="Play" UnloadedBehavior="Stop" Source="{Binding Path=CurrentlyPlaying}" />
    <ListBox Name="lstSongs" ItemsSource="{Binding Path=SongList}" SelectionMode="Extended" Grid.Row="1" />
    <Button Content="Play selected" Click="cmdQueueItems_Click" Grid.Row="2" />
    <Button Content="Skip" Click="cmdSkipItem_Click" Grid.Row="3" />
</Grid>