如何选择并播放列表中的多首歌曲?

发布于 2024-12-01 18:11:33 字数 153 浏览 2 评论 0原文

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

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

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

我会感谢你的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎夜雨 2024-12-08 18:11:33

最简单的方法是使用一个 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);
}

(确保 CurrentPlaying 属性引发 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文