Reputation: 1
I have a requirement, 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 list box. User should able to select the multiple songs from the list box and click play.
I have to play the all audio songs selected by user one by one.
I will appreciate your help.
Upvotes: 0
Views: 1222
Reputation: 2513
The easiest way to do this would be to have a ListBox control with its ItemsSource property bound to some collection ("SongList" for example).
<ListBox Name="lstSongs" ItemsSource="{Binding Path=SongList}" SelectionMode="Extended" Grid.Row="1" />
Have a MediaElement control on the page listening for the next song
<MediaElement Name="player" MediaEnded="MediaElement_MediaEnded" LoadedBehavior="Play" UnloadedBehavior="Stop" Source="{Binding Path=CurrentlyPlaying}" />
When the user clicks a button on the form, the currently selected items are added to a queue. The currently selected items can be found by reading the ListBox.SelectedItems property.
private void cmdQueueItems_Click(object sender, RoutedEventArgs e)
{
Queue = lstSongs.SelectedItems.OfType<Uri>().ToList();
playNext();
}
You then start playing the first item in the queue, and when the MediaElement.MediaEnded
event is raised, replace the currently playing item with the next item in the queue if there is one available.
private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
playNext();
}
You would do this until the user hit a predefined stop button.
The playNext()
method would just simply be
private void playNext()
{
CurrentlyPlaying = Queue.FirstOrDefault();
if (CurrentlyPlaying != null)
Queue.Remove(CurrentlyPlaying);
}
(Make sure that the CurrentlyPlaying property raises the INotifyPropertyChanged.PropertyChanged
event)
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>
Upvotes: 1