2011-08-29 50 views
0

我有一个要求,我需要在窗口上使用wpf播放多个音频歌曲。任何人都可以提出实现此目的的最佳方法。如何选择和播放列表中的多首歌曲?

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

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

我会感谢您的帮助。

回答

1

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

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

有下一首歌曲

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

页面收听一个MediaElement控件当用户点击窗体上的按钮,当前选择的项目添加到队列中。通过阅读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); 
    } 
} 

主窗口.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> 
+0

我得到队列附近错误。什么应该怎么办?请帮我 – Radhika

+0

我选择了2首歌曲从列表框中,我加入到队列中,然后我点击播放按钮...但它是不是在玩...我该怎么办? – Radhika

+0

您是否正确设置了绑定? Uri是否有效?我已经用完整源代码更新了我的帖子。 – fatty

相关问题